CodeIgniter Forums
Undefined model - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Undefined model (/showthread.php?tid=50320)



Undefined model - El Forum - 03-22-2012

[eluser]Martenvanurk[/eluser]
Dear,

I'm using the following code:

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

$this -> load -> model ('MenuModel');
$this -> MenuModel -> setAcl ( $this -> AclModel );

This is my model:
Code:
class MenuModel extends CI_Model {
    /**
     * Acces control list object
     * @var object
     */
    private $acl;

    public function setAcl( &$acl )
    {
        $this->acl = $acl;
    }
}

When I create an error in the model, CI report it fine. So the model is loaded normal.
But when I use the code in the first block in my controller I got the following error:

Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined property: Menu::$MenuModel

Filename: controllers/Menu.php

Line Number: 36

Line 36 is this line ( first code block): $this -> MenuModel -> setAcl ( $this -> AclModel );

In the code before this, I loaded the MenuModel without any problems. Will this cause the error? Think CI will use the Singleton or Dependency Injection container though.

How can I fix this error, the syntax is right I think.....

Kind regards!
Marten


Undefined model - El Forum - 03-22-2012

[eluser]gRoberts[/eluser]
It's likely the model isn't actually getting loaded correctly.

As a test, try

Code:
$AclModel = $this->model->load('AclModel')
$MenuModel = $this->model->load('MenuModel');
$MenuModel->setAcl($AclModel);



Undefined model - El Forum - 03-22-2012

[eluser]Martenvanurk[/eluser]
mmmm. working now..... How can you explain this?


Undefined model - El Forum - 03-22-2012

[eluser]gRoberts[/eluser]
This confirms my initial suspicions.

Code:
$this->model->load('model_model');
will set $this->model_model and also return an instance of the model when you call it.

If neither are being set, it appears your model is not loading. Do you have other models that work? It may be related to your naming convention. I think it should be "Acl_model" and "Menu_model" although I have never tried it any other way, so it may work.

Within both of your defined models, are you calling
Code:
parent::__construct();
in your constructor? It could be that your not initialising the underlying model class and it's refusing to load correctly?


Undefined model - El Forum - 03-22-2012

[eluser]Martenvanurk[/eluser]
About the naming convention, there are more models which are working well. Even the same menu module is loaded earlier and works well.

I always loads the contructor....


Undefined model - El Forum - 03-22-2012

[eluser]gRoberts[/eluser]
Sorry to sound like I was questioning your ability, I just had to ask just in case.

Code:
var_dump($this->MenuModel);

What does the above produce?


Undefined model - El Forum - 03-22-2012

[eluser]Martenvanurk[/eluser]
null it is Smile