Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] "Non-object" fatal error when loading two models in a controller
#1

[eluser]adityamenon[/eluser]
Hi all, I have seen plenty of code examples where there are two models freely loaded and used like so:

Code:
$this->load->model('model1');
$this->load->model('model2');

$this->model1->someFunction();
$this->model2->someOtherFunction();

But when I load two models in my code, CI claims that the second model object is non-existent. This is my code:

Code:
class Main extends CI_Controller {

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

    function index()
    {
        $this->load->model('head_section_model');
        $this->load->view('head_section', array('title' => 'Home - Site X', 'marquee' => $this->head_section_model->get_marquee()));
        $this->load->view('home_page', array('X_data' => $this->_data('X'), 'Y_data' => $this->_data('Y')));
        $this->load->view('footer');
    }
    
    private function _data($type)
    {
        $this->load->model('frontend_model', 'frontend');
        return $this->frontend->home_data($type);
    }
}

When I stop loading any one model, the code functions normally. But when I run the code above, I'm receiving an error like so:

Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: Main::$frontend

Filename: controllers/main.php

Line Number: 21

Fatal error: Call to a member function home_data() on a non-object in C:\Program Files\wamp\www\ongoing\SiteX\application\controllers\main.php on line 21

I'm pretty sure I'm missing something fundamental... can someone please help out?
#2

[eluser]adityamenon[/eluser]
Actually, I don't absolutely NEED this answer right now, but it would still be nice to know why that was happening.

Why don't I need the answer? I realized that it is moot to call your model a "Frontend_model" if you're going to use ANOTHER model to simply fetch <marquee> data for... the *frontend*, LOL. Basically, one model per controller is enough in this case... but still, curiosity gnaws.
#3

[eluser]InsiteFX[/eluser]
Try loading your models in your Constructor!

InsiteFX
#4

[eluser]adityamenon[/eluser]
I did that! The result is the same. I'm getting the same error. If I comment out one model, the other one works. But loaded together, the "non-object" error appears.
#5

[eluser]vitoco[/eluser]
i don't know if this may help, but in this line
Code:
$this->load->view('home_page', array('X_data' => $this->_data('X'), 'Y_data' => $this->_data('Y')));

you call the "_data" function twice, so the model it's loaded two times also...you get the "... on a non-object" error in the first or second call ???
#6

[eluser]cideveloper[/eluser]
Can we see your frontend_model?

I loaded up the exact code you showed and it works. Obviously I just made up what the models return.

Could this be of help?
#7

[eluser]adityamenon[/eluser]
Hi, CIDeveloper thanks for trying to help. Here's the code for the model:

Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Frontend_model extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        $this->load->database();
    }
    
    //X and Y data on the home page
    function home_data($type)
    {
        $return = 'none found';
        $this->db->order_by('last_changed', 'DESC');
        $query = $this->db->get($type, 4);
        if($query->num_rows() > 0)
        {
            $return = $query->result_array();
        }
        return $return;
    }

    //get the marquee above the navigation
    function get_marquee()
    {
        $return = 'none found';
        $this->db->order_by('timestamp', 'DESC');
        $query = $this->db->get('home_news');
        if($query->num_rows() > 0)
        {
            $return = $query->result_array();
        }
        return $return;
    }
    
    //X and Y data in the dedicated pages
    function all_type_data($type)
    {
        $return = 'none found';
        $this->db->order_by('last_changed', 'DESC');
        $query = $this->db->get($type);
        if($query->num_rows() > 0)
        {
            $return = $query->result_array();
        }
        return $return;
    }
}

^^As you can see, the above is pretty innocent stuff, and there are no errors coming out of there.

Like I had stated, I had abandoned trying to load two models in the same controller after I faced this error. However, I am facing a new, but somehow related problem.

I'm trying to use the pagination library, but it won't execute the create_links() function inside the view. I have already put this line into application/config/autoload.php

Code:
$autoload['libraries'] = array('form_validation', 'session', 'pagination', 'database');

All the other libraries are working fine. Except for Pagination and Database. As you can see, I had to include this line inside the __constructor function in the frontend_model:

Code:
$this->load->database();

I'm unable to use any db functions otherwise! CI says $this->db is an unidentified object.

And coming back to pagination, here's the controller code:

Code:
function index($type)
    {
        $this->load->view('head_section', array('title' => 'Browse all '.$type.' - Site name', 'marquee' => $this->frontend_model->get_marquee()));
        
        $pag_config['base_url'] = base_url().'find_partner/index/'.$type.'bml';
        $pag_config['total_rows'] = 4;
        $pag_config['per_page'] = 2;
        $this->pagination->initialize($pag_config);
        
        $this->load->view('find_partner', array('data' => $this->_data($type), 'type' => $type));
        $this->load->view('footer');
    }

The Pagination initialize() function is working in the controller, but in the view,

Code:
<div id="mainbody">
                <div id="inner-page-box">
                    <img src="&lt;?=base_url()?&gt;assets/images/inner-page-box/bg-top.png" id="inner-page-box-top" /><img src="&lt;?=base_url()?&gt;assets/images/inner-page-box/bg-btm.png" id="inner-page-box-btm" />
                    <h1>All  &lt;?=$type?&gt; with Site X</h1>
                    <p>
                        &lt;?php echo $this->table->generate($data); ?&gt;
                        &lt;?php echo $this->pagination->create_links(); ?&gt;
                    </p>
                </div>
            </div>

^^ In the above create_links() is not working. The error I'm getting is:

Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: CI_Loader::$pagination

Filename: views/find_partner.php

Line Number: 8

Can anyone tell me what's wrong? I even changed the config['uri_protocol'] from 'QUERY_STRING' to 'AUTO' to see if that was causing the problem, but there was no result...

Thanks a lot for reading my long message!
#8

[eluser]InsiteFX[/eluser]
Code:
// this
$pag_config

// should be
$config

// change the order of the autoload!
$autoload['libraries'] = array('database', 'session', 'form_validation', 'pagination');

InsiteFX
#9

[eluser]adityamenon[/eluser]
Hi InsiteFX!

Thanks for the answer, I did what you had suggested, but no luck on both counts Sad

Even after changing $pag_config to $config, the view is not rendering fully, it's stopping at the place I wrote create_links(); and saying

Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: CI_Loader::$pagination

Filename: views/find_partner.php

Line Number: 8

And even after changing the order of the $autoload['libraries'] like you had suggested (copy-pasted your code I swear), the model stops working if I don't write $this->load->database(); in the constructor. It says:

Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: Frontend_model::$db

Filename: models/frontend_model.php

Line Number: 28

I appreciate your time, everybody... please help me pass this storm Smile It's really annoying how a lot of work gets stopped with really small errors like this one!
#10

[eluser]InsiteFX[/eluser]
How did you save your model files? It sounds like you have a naming error!

InsiteFX




Theme © iAndrew 2016 - Forum software by © MyBB