A new problem with sidemenu in header block - php error

edited June 2012 in News & Announcements
Ok, I am really losing hair here :(

In my header block I have this code:
$this->load->view('_blocks/sidemenu');

My sidemenu block has this code:
$cats = fuel_model('categories', array('find' => 'all', 'where' => array('published' => 'yes'), 'title asc')); $sidemenu = array(); foreach($cats as $cat): $id = $cat->id; $title = $cat->title; $total = fuel_model('categories_to_lists',array('find' => 'record_count', 'where' => array('cat_id' => $id))); $sidemenu['lists/'.$title] = $title . "($total)"; endforeach; $menu = $this->menu->render($sidemenu, '', NULL, 'collapsible'); echo $menu;

I have created modules like the ones in the Simple Module example but for my site I am not using an authors module.

My modules:
categories_model
lists_model

My example.php is called: lists.php

The header works fine on the home page and on the lists page, BUT, if I try to call the lists page with a category like so:
mysite/lists/accomodation

The header does not appear and I get this error:

A PHP Error was encountered

Severity: Notice

Message: Undefined property: CI_Exceptions::$load

Filename: _blocks/header.php

Line Number: 68

( ! ) Fatal error: Call to a member function view() on a non-object in C:\wamp\www\clarenssa\fuel\application\views\_blocks\header.php on line 68

Why? :(

Comments

  • edited 10:58PM
    I moved the loading of the sidemenu block to the main layout file, and into the home.php file.

    Working fine on home, mysite/lists

    On mysite/lists/accommdation:

    I get the error page not foud where the side menu is supposed to be.
  • edited 10:58PM
    Tried that in my main layout:
    <?php echo fuel_block('sidemenu'); ?>

    Page not found

    Looking at the help for fuel_block:
    Allows you to load a view and pass data to it. The params parameter can either be string value (in which case it will assume it is the name of the block view file) or an associative array that can have the following values:

    If I call my block like that:

    echo fuel_block('view'=>'sidemenu', 'model'=>'categories', 'find'=>'all', 'order'=>'title asc');

    Then what? What do I do in the block view? how do I get the details from the model?

    I really don't understand.

    I think I am missing some basic understanding in this system.

    I don't know where to look anymore.

    What am I missing?
  • edited 10:58PM
    When you see something like

    CI_Exceptions::$load

    It generally points to the wrong '$this'. Try getting a CI instance with $CI =& get_instance()

    And using that to load, like:

    $CI->load->view()

    Little bit odd that it's the exceptions class in your example though. Makes me wonder if you have a 404/error/routes problem ahead of this.
  • edited 10:58PM
    Thanks, not working.

    I have this now in my main layout file:
    <?php $this->load->view('_blocks/header')?> <div id="sidebar"> <?php $CI =& get_instance(); $CI->load->view('_blocks/sidemenu'); ?> </div> <div id="content"> <?php echo fuel_var('body', ''); ?> </div> <?php $this->load->view('_blocks/footer')?>

    mysite/lists/accomodation

    Instead of a side menu I am getting: Page not found.

    Why does it see the header and footer but not the sidemenu block?
  • edited 10:58PM
    It sounds like you are getting a 404 error page. By default the header and footer are pulled into the 404 error page (see the fuel/application/errors/error_404.php). Is there a view file or controller that serves up that page?
  • edited 10:58PM
    Yes, but why do I get 404 error page?

    That's the question.

    Why is it working fine on: mysite/lists and not on mysite/lists/accommodation?

    I am following the Create Simple Module from the userguide.

    I don't get page not found there, why do I get it in this site?
  • edited 10:58PM
    Ok, I am going to give it another try.
    I'm going to create a fresh new site, and copy all the code from the example.

    Then I'll start tweaking it again to my needs, and will let you know what's happenning.

    Hopefully, it will work this time :)
  • edited 10:58PM
    While I'm working on redeveloping, can you suggest the way to do the sidemenu the right way please?

    Can you give me code example?

    Thanks
  • edited 10:58PM
    I don't think your current issue in this thread is related to your block. It seems you are 404-ing trying to get to mysite/lists/accommodation.

    What is lists/accomodation? Is that the location field of a cms page or a controller, method (lists::accomodation) or a directory/file?

    FUEL provides a number of ways to create pages and hierarchy. The fuel_mode config variable in application/config/MY_fuel.php helps control this. What's yours set to?

    Here's some example code from one of our blocks. All these pages are cms pages and the block is part of the layout they use. The block uses the url segment to get it's child nav links:

    echo fuel_nav(array('parent' => uri_segment(1)));

    Crazy simple right? That renders a
      list of the current pages child nav links.
  • edited 10:58PM
    Thanks Lance, thank you, thank you, thank you, can't say it enough.

    The problem was in MY_fuel, on this line:
    $config['auto_search_views'] = TRUE; It was set to false.
  • edited 10:58PM
    Ok, I have restarted everything.

    Created categories_model, members_model, categories_to_members model.

    First question:

    I want to build a sidemenu that displays all the categories names from the database.

    Do I create my sidemenu block view like this:
    $cats = fuel_model('categories', array('find' => 'all', 'where' => array('published' => 'yes'), 'name asc')); $sidemenu = array(); foreach($cats as $cat): $id = $cat->id; $title = $cat->name; $total = fuel_model('categories_to_members',array('find' => 'record_count', 'where' => array('category_id' => $id))); $sidemenu['lists/'.$title] = $title . "($total)"; endforeach; $menu = $this->menu->render($sidemenu, '', NULL, 'collapsible'); echo $menu;

    Do I need to use 'published'=>'yes'? Doesn't it automatically get only the published ones?

    I tried to understand what fuel_block is doing so I put this code in my main layout file: echo fuel_block(array('view'=>'sidemenu', 'model'=>'categories', 'find'=>'all', 'order'=>'name asc'));
    But that doesn't do anything.

    So is the first way the right way to go? When and how do I use fuel_block?

    If you can give me a code example that will be great.
  • edited 10:58PM
    Second question:
    In my categories table I have a record: id = 1, name = Accommodation
    In my members table I have a record: id = 1, title = Test
    In my categories_to_members table I have a record: category_id = 1, member_id = 1.

    My categories_to_members model:
    class Categories_to_members_model extends MY_Model { public $record_class = 'Category_to_member'; public $foreign_keys = array('category_id' => 'categories_model', 'member_id' => 'members_model'); function __construct() { parent::__construct('categories_to_members'); } function _common_query() { $this->db->select('categories_to_members.*, members.title, categories.name AS category_name, categories.published'); $this->db->join('members', 'categories_to_members.member_id = members.id', 'left'); $this->db->join('categories', 'categories_to_members.category_id = categories.id', 'left'); } } class Category_to_member_model extends Data_record { public $category_name = ''; public $title = ''; }
    Yet, this line in my sidemenu code gives me a blank result:
    $total = fuel_model('categories_to_members',array('find' => 'record_count', 'where' => array('category_id' => $id)))

    I echoed the category id and it is: 1

    Why is the $total blank and not 1?
  • edited 10:58PM
    Third question:

    The code in the memebers.php view file:
    <?=$member->content_formatted?> does not print anything to the page.

    I added this to my Members_model:
    public $parsed_fields = array('content', 'content_formatted');
    Still nothing is printed. This problem exists on your Simple Modules guide as well.

    How do I fix this please?
  • edited 10:58PM
    For your second question, try using just the model instead of the fuel_model function. That function has limited methods it can run on it (the find methods) and is just an alias to the model's methods. The reason is because it is one of the functions that can be used in the CMS Dwoo templating syntax and so we limit what it can do (can't delete records as well as call some of the other methods). Try the following:
    $CI->load->model('categories_to_members_model'); $CI->categories_to_members_model->record_count();

    For question #2, does your members model have a content field and is there anything in it (I didn't see what fields the members model had)?
  • edited 10:58PM
    Thank you admin, I will try your suggestions.

    My members table has content, address, phone and web site fields. All the fields have data in them.

    My members_model is just a copy of the articles model in the simple module tutorial.

    The code:
    <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); require_once(FUEL_PATH.'models/base_module_model.php'); class Members_model extends Base_module_model { public $required = array(); public $parsed_fields = array('content', 'content_formatted'); function __construct() { parent::__construct('members'); // table name } function list_items($limit = NULL, $offset = NULL, $col = 'title', $order = 'asc') { $this->db->select('members.id, title, SUBSTRING(content, 1, 50) AS content, date_added, members.published', FALSE); $data = parent::list_items($limit, $offset, $col, $order); return $data; } function tree() { $CI =& get_instance(); $CI->load->model('categories_model'); $CI->load->model('categories_to_members_model'); $return = array(); $categories = $CI->categories_model->find_all(array(), 'id asc'); $categories_to_members = $CI->categories_to_members_model->find_all('', 'categories.name asc'); foreach($categories as $category) { $cat_id = $category->id; $return[] = array('id' => $category->id, 'label' => $category->name, 'parent_id' => 0, 'location' => fuel_url('categories/edit/'.$category->id)); } $i = $cat_id +1; foreach($categories_to_members as $val) { $attributes = ($val->published == 'no') ? array('class' => 'unpublished', 'title' => 'unpublished') : NULL; $return[$i] = array('id' => $i, 'label' => $val->title, 'parent_id' => $val->category_id, 'location' => fuel_url('members/edit/'.$val->member_id), 'attributes' => $attributes); $i++; } return $return; } function form_fields($values = array()) { $fields = parent::form_fields(); $CI =& get_instance(); $CI->load->model('categories_model'); $CI->load->model('categories_to_members_model'); $category_options = $CI->categories_model->options_list('id', 'name', array('published' => 'yes'), 'name'); $category_values = (!empty($values['id'])) ? array_keys($CI->categories_to_members_model->find_all_array_assoc('category_id', array('member_id' => $values['id'], 'categories.published' => 'yes'))) : array(); $fields['categories'] = array('label' => 'Categories', 'type' => 'array', 'class' => 'add_edit categories', 'options' => $category_options, 'value' => $category_values, 'mode' => 'multi'); return $fields; } // add categories function on_after_save($values) { $data = (!empty($this->normalized_save_data['categories'])) ? $this->normalized_save_data['categories'] : array(); $this->save_related('categories_to_members_model', array('member_id' => $values['id']), array('category_id' => $data)); } // cleanup members from categories to members table function on_after_delete($where) { $this->delete_related('categories_to_members_model', 'member_id', $where); } } class Member_model extends Data_record { } ?>
    I just copied the articles model and removed the author related code since I don't use authors.

    I installed all the files from the simple module tutorial and created a testing site for it, and the content of the article does not appear there either.
  • edited 10:58PM
    Is your "member" object originating from the members model or the categories_to_members to model?
  • edited 10:58PM
    It is coming from the categories model as far as I can understand the code.

    The url is: mysite/members/Accommodation

    The member file:
    <?php $category = $CI->uri->segment(2); if (!empty($category)) { $CI->load->model('categories_model'); // remember, all categories that have a published value of 'no' will automatically be excluded $category = $CI->categories_model->find_one_by_name($category); $members = $category->members; } else { $CI->load->model('members_model'); // remember, all members that have a published value of 'no' will automatically be excluded $members = $CI->members_model->find_all(); } ?> <h1><?php if (!empty($category)) : ?> <?=$category->name?> <?php endif; ?></h1> <div style="margin-top: 10px;"> <?php foreach($members as $member) : ?> <div class="member"> <h2 class="member"><?=fuel_edit($member->id, 'Edit: '.$member->title, 'members')?><?=$member->title?></h2> <p>Address: <?=$member->address?></p> <p>Phone: <?=$member->phone?></p> <p>Web site: <a href="http://<?=$member->site?>" target="_blank"><?=$member->site?></a></p> <p> <?=$member->content_formatted?> </p> </div><!-- End member--> <?php endforeach; ?> </div>

    If I call the content like this:
    <?=$member->content?>
    Then it displays on the page. But using your example from Simple Module, with content_formatted, it does not work.

    I've added this line to all my models(members, categories, categories_to_members):
    public $parsed_fields = array('content', 'content_formatted');

    Also how do I add paragraphs to the content? Is that what the content_formatted supposed to do?
  • edited 10:58PM
    By the way, your answer for my $total problem helped, thanks.

    So now I only need solution for the content_formatted and if possible, an answer for my first question above.

    Thank you :)
  • edited 10:58PM
    If I add the

    tags in the table itself, it breaks the text to paragraphs but how do I add paragraphs in the admin form?

    If I just do enter it doesn't add a parapgraph.

  • edited 10:58PM
    For the paragraphs, it depends on what editor you have configured in the Admin. If you are using markItUp! which is the default, then you need to add them. The toolbar has an easy way to do that (the "P" toolbar button). You can set the editor to be the CKEditor as well by changing the MY_fuel.php "text_editor" config setting like so:
    $config['text_editor'] = 'ckeditor';
  • edited 10:58PM
    Thank you, that helped.
Sign In or Register to comment.