Can you access a model from inside another model in CodeIgniter?


I am writing a webapp using CodeIgniter that requires authentication. I created a model which handles all my authentication. However, I can't find a way to access this authentication model from inside another model. Is there a way to access a model from inside another mode, or a better way to handle authentication inside CodeIgniter?

7/21/2012 10:17:24 AM

Accepted Answer

In general, you don't want to create objects inside an object. That's a bad habit, instead, write a clear API and inject a model into your model.

// in your controller
$model1 = new Model1();
$model2 = new Model2();
9/5/2008 5:26:45 PM

It seems you can load models inside models, although you probably should solve this another way. See CodeIgniter forums for a discussion.

class SomeModel extends Model
  function doSomething($foo)
    $CI =& get_instance();

    // use $CI instead of $this to query the other models

Also, I don't understand what Till is saying about that you shouldn't create objects inside objects. Of course you should! Sending objects as arguments looks much less clear to me.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow