2

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

10
  • Are you sure that your model is not stored in subfolder for example models->subfolder->your_model Commented Jan 13, 2013 at 15:47
  • No, both are stored in /application/models/model_name.php :/ If not, they would not be loaded and work individually Commented Jan 13, 2013 at 15:58
  • What happens when you ONLY load model_annonce ? Does it work properly then? Commented Jan 13, 2013 at 16:16
  • Have you tried without the second param in load->model and calling $this->model_annonce->listerEspeces()? Commented Jan 13, 2013 at 17:01
  • Also, if you're using __construct() in any of your models, remember to add parent::__construct() in your __construct() method which may be causing this error. Commented Jan 13, 2013 at 17:02

2 Answers 2

4

Please try the following way...

 public function index()
{
    $this->load->model(array('membre','annonce'));
    $liste = $this->annonce->listerEspeces();
    $membre = $this->membre->obtenirMembre(1);
}
Sign up to request clarification or add additional context in comments.

4 Comments

I tried the following without success, the same error, call to a member function listerEspeces() on a non-object. [code]public function index() { $this->load->model(array('model_membre', 'model_annonce')); $liste = $this->model_annonce->listerEspeces(); $membre = $this->model_membre->obtenirMembre(1); }[/code]
@OwNAkS - then your error might be coming from another part of your code.
@mamdouhalramadan It is not possible because I made a controller just for testing, empty, with just the constructor, and the function written above. I dont have any models loaded in my autoload.php.
then try loading both models from autoload :)
2

The models should extend CI_Model and not CI_Controller! This was the source of the problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.