Php – How to load two models in the same controller with CodeIgniter

codeigniterPHP

I've been searching on Internet and on stackoverflow, but i didn't find a solution, or it wasn't the same problem.

I need to load two models in the same controller, and in the same function. One is "model_membre" and the other "model_annonce". It's the name of the class, the file, and the object.

Individually, they work very good, i can access to properties and methods, but when I'm loading the two models, I can't access to the second, regardless of how I load it (autoload, $this->load->model('model_name'), $this->load->model('model_name', 'object_name')).

I simplified to a "test" controller, to see if it was not another part of my code that made the problem :

    public function index()
{
    $this->load->model('model_membre', 'membre');
    $this->load->model('model_annonce', 'annonce');
    $liste = $this->annonce->listerEspeces();
    $membre = $this->membre->obtenirMembre(1);
}

I tried to changer the order, and the 2nd loaded never works. I get this message :

A PHP Error was encountered
Severity: Notice
Message: Undefined property: Test::$annonce
Filename: controllers/test.php
Line Number: 15

Fatal error: Call to a member function listerEspeces() on a non-object in /homez.604/animalix/beta/application/controllers/test.php on line 15

Best Answer

Please try the following way...

 public function index()
{
    $this->load->model(array('membre','annonce'));
    $liste = $this->annonce->listerEspeces();
    $membre = $this->membre->obtenirMembre(1);
}
Related Topic