How to create a model that doesn't rely on being a module?

edited October 2015 in Modules
I'm trying to create a model for my sidebar, so I can put all the logic for getting data in there and not in each view's controller / file. Also I don't want to show certain parts of my sidebar in certain views (hiding the upcoming event on the homepage, for example).

So I have fuel/application/models/sidebar_model.php as something like:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); require_once(FUEL_PATH.'models/base_module_model.php'); class Sidebar_model extends Base_module_record { public function get_recent_posts($num = 5) { $recent_posts = array(); // .. logic return $recent_posts; } public function get_recent_comments($num = 5) { $recent_comments = array(); // .. logic return $recent_comments; } public function get_upcoming_event() { // .. logic example $today = date("Y-m-d H:i:s"); $event = fuel_model('events', array( 'find' => 'one', 'order' => 'sticky, start_date desc', 'where' => "end_date >= '$today'", 'module' => 'events', 'return_method' => 'object' )); return $event' } } ?>

and I have it autoloaded in fuel\application\config\autoload.php:

$autoload['model'] = array( 'sidebar_model' );

and then I call it from each controller, for example in the home controller:

$sidebar_model = $this->sidebar_model; $vars['sidebar'] = array(); $vars['sidebar']['recent_posts'] = $sidebar_model->recent_posts; $vars['sidebar']['recent_comments'] = $sidebar_model->recent_comments;

and I plan to in the view, include the sidebar with the sidebar array ($this->load->view('sidebar', $sidebar);) for example. Although now the problem is with this code, I get this error in the home view:

A PHP ERROR WAS ENCOUNTERED Severity: Notice Message: Trying to get property of non-object Filename: core/MY_Model.php Line Number: 5572 A PHP ERROR WAS ENCOUNTERED Severity: Notice Message: Trying to get property of non-object Filename: core/MY_Model.php Line Number: 5573 A PHP ERROR WAS ENCOUNTERED Severity: Notice Message: Trying to get property of non-object Filename: core/MY_Model.php Line Number: 5572 A PHP ERROR WAS ENCOUNTERED Severity: Notice Message: Trying to get property of non-object Filename: core/MY_Model.php Line Number: 5573

The data is coming through fine, I'm getting the data back for the recent posts and recent comments from the sidebar model, although I just get this error. so am I missing out on something? How best is it to create a stand-alone model?

Comments

  • If it's a generic CodeIgniter model that doesn't need the Fuel bits, you don't need the require_once(FUEL_PATH.'models/base_module_model.php'); class Sidebar_model extends Base_module_record {
    Perhaps if you declare the model as class Sidebar_model extends CI_Model {
  • edited October 2015
    Well, I'm using fuel_model to get the data from fuel, and also the fuel_blog module $recent_posts = $this->_CI->fuel_blog->get_recent_posts(3);. Is that what you mean by using fuel? Because if so, then yes :p
  • edited 7:28PM
    Okay I've figured it out, although I still have an issue.

    The original problem was the way I was calling the data.

    Instead of $vars['sidebar']['recent_posts'] = $sidebar_model->recent_posts; I should be doing $vars['sidebar']['recent_posts'] = $sidebar_model->get_recent_posts();. I guess the get_ prefix isn't translating well.

    Now the other problem I have is that I have 3 views, home, event, and location.

    On the home view, in the sidebar model $this->_CI->fuel_blog is defined, however on the other views sidebar_model reports that it's null. For example I have line 11 which is $recent_posts = $this->_CI->fuel_blog->get_recent_posts(3);, running this on the home view is fine, although on the event or location view I get:

    Fatal error: Call to a member function get_recent_posts() on null in fuel\application\models\sidebar_model.php on line 11

    What's up with that? How can I find the fuel_blog from here?
  • edited 7:28PM
    Try running $this->_CI->fuel->blog->get_recent_posts(3), instead of using fuel_blog. This method will dynamically load it if it doesn't exist.
  • edited 7:28PM
    Oh, yeah that fixed it. Thank you so much! :)
Sign In or Register to comment.