Accessing CodeIgniter Models in Other Models Accessing CodeIgniter Models in Other Models codeigniter codeigniter

Accessing CodeIgniter Models in Other Models


Those are some VERY long answers for a simple question.

Short answer: This is now fully supported. Cross-load away if you feel like it!


I strongly disagree with the notion that a "model" should only encapsulate a database table with simple CRUD operations. As noted in the Wikipedia article:

http://en.wikipedia.org/wiki/Model-view-controller#As_a_design_pattern

...that application layer is intended to do more than simply act as a single database table abstraction. Think about the meaning of the word "controller" -- it should act more as a director rather than being the entire application in and of itself. A "model" is a place for business logic. Most large scale applications in fact hold much of their business logic in the database itself (in the form of triggers, stored procedures, foreign keys, etc.).

I think the misunderstanding of what a "model" is is partly caused by the same (over-)hype of "MVC", without carrying with it much understanding of the concepts themselves. Kinda like how empty "AJAX" is, or even more easy, "Web 2.0". For better or worse, plenty of script kiddies have jumped on the MVC wagon, and since the simple howtos and example scenarios don't do much more than tell you to put your database code in the "model", the misuse of that layer as only a database abstraction has become commonplace. Now you read posts all over the Internet calling it "unpure", "dirty", "hackish" to put any business logic in a model. That's WRONG. Misinformed.

The easy example is to think about foreign keys: even if you only want your "model" to be a database model, if you want to be "pure", "correct" or what have you, you really should be enforcing referential integrity therein. Thanks to the lack of real foreign key support in MySQL for many years, web applications grew up without anyone worrying about referential integrity at all. Fits the script kiddie lifestyle I guess. Anyhow, for even this simplified view of a model to be able to maintain foreign key validity, a model then has to work with others (or, especially if a framework like CodeIgniter does not let you do so, you have to write queries to other tables, sometimes duplicating queries elsewhere - THAT is bad style).

Therefore, I believe this to be a shortcoming of CodeIgniter. I understand that it might not be an easy fix, but it's certainly a disappointing oversight.

So what I did was take the example code above and abstract it into a helper so that I now have a function that works almost identically to the normal $this->load->model() functionality. Here it is (put it into a helper that is auto-loaded and you can use it in any model):

   /**    *    * Allow models to use other models    *    * This is a substitute for the inability to load models    * inside of other models in CodeIgniter.  Call it like    * this:    *    * $salaries = model_load_model('salary');    * ...    * $salary = $salaries->get_salary($employee_id);    *    * @param string $model_name The name of the model that is to be loaded    *    * @return object The requested model object    *    */   function model_load_model($model_name)   {      $CI =& get_instance();      $CI->load->model($model_name);      return $CI->$model_name;   }


It's possible, but not ideal and considered bad and more for "quick fix" than ideal or pure implementation.

class Location extends Model{      public function get($ID){                // Get main CI object handle and load model                $CI =& get_instance();                $CI->load->model('LocationType');                // Call new model functions using handle to main CI object                $CI->LocationType->setID($result->LocationTypeID);                $CI->LocationType->setTitle($result->TypeTitle);                $this->_locationType = $CI->LocationType;                //Other Stuff    }}

Anytime you're using the main CI object like this is probably a bad idea. Try to re-think your layout and just pass data to/from your controller to the models.

http://codeigniter.com/forums/viewthread/69833/