Loading the Model In Controller

edited October 2013 in Share
I'm kind of unclear as to how to do this with FUEL. I decided to use the regular CodeIgniter controller/view process of displaying information. So referencing the model from the controller should be as simple as $this->load->model('foo'). But for some reason it isn't working. I'm calling this from within the index() method of the CI_Controller (or the class that extends it).

Comments

  • edited 11:15PM
    That should work. What is the name of your model?
  • edited 11:15PM
    The name of the model is "categories_model". I used the fuel articles, category setup for this project because it involved writing, storing, and categorizing articles. So I just pulled from the FUEL tutorials. The difference though is that I want to use the basic CodeIgniter MVC relationship to display things.

    Here is my controller.

    <?php

    class ArtsJournals extends CI_Controller{

    function __construct()
    {
    parent::__construct();
    }


    function index()
    {

    // use Fuel_page to render so it will grab all opt-in variables and do any necessary parsing
    $page_init = array('location' => 'artsjournals', 'render_mode'=>'cms');

    $this->load->module_library(FUEL_FOLDER, 'fuel_page', $page_init);
    $this->fuel_page->add_variables(array('layout'=>'pagelayout'));
    $this->fuel_page->render();

    $this->load->model('categories_model');

    $data = array();

    // remember, all categories that have a published value of 'no' will automatically be excluded
    $categories = $this->categories_model->find_all();


    foreach($categories as $category)
    {
    $data[$category->name]=array();

    foreach($category->articles as $article)
    {
    $data[$category][$article->title] = array
    (
    "title" => $article->title,
    "content" => $article->content,
    "date_added" => $article->date_added,
    "image" => $article->article_image,
    "subcontent" => $article->pulloutquote
    );
    }
    }

    $this->load->view("artsjournals", $data);

    //echo $categories;


    }


    }

    Not sure why this isn't working.
  • edited 11:15PM
    What kind of errors are you seeing?

    Also, as an FYI, FUEL 1.0 beta has an updated tutorial using the 1.0 branch (which has ton of extra features including automatic relationships with has_many and belongs_to).
    https://github.com/daylightstudio/FUEL-CMS/tree/1.0
    http://docs.getfuelcms.com/modules/tutorial
    http://docs.getfuelcms.com/general/models
  • edited 11:15PM
    It's kind of odd and I probably can attribute this to my being new to CodeIgniter. From my understanding you are suppose to be able to load any model which is a reference to table data.

    So if my model is:

    class Foo extends Base_module_model
    {

    public customFunction(){

    }
    }

    saved in file foo.php in the "models" directory.

    I'm suppose to be able to call that function in the controller like:

    $foomodel = $this->load->model('foo')

    $this->foomodel->customFunction();

    At least that how I thought it worked. Correct?
  • edited 11:15PM
    For the model to load correctly, you need to have a file called categories_model.php located in the fuel/application/model directory. That file should contain the class name of Categories_model.php and should extend Base_module_model similar to your example above. The constructor should pass the name of the table to the parent constructor. Is that your setup?
  • edited 11:15PM
    Okay. I was just referencing the model the wrong way.

    I had:

    $categories = $this->load->model('categories_model');

    then..

    foreach ($categories as $category) ..etc

    I wasn't suppose to assign the model class to a
Sign In or Register to comment.