views:

2284

answers:

6

It used to be that within a CodeIgniter model you couldn't access another model. E.g

$this->load->model('bar');
$this->bar->something();

Would be invalid within the code of a model. Is this still valid or have they changed it?

+1  A: 

I think it's generally better the write libraries that access the models and then include the libraries in your model if need be.

For instance, if you needed to check if someone is authorized to go through with a certain CRUD action, you might want to include whatever authentication library you are using (its probably auto-included in most cases). You wouldn't necessarily want to access the model directly--it just seems dirty and improper.

I think the preferred way is to do what you need to do in your controller and pass the results from one model's method(s), if need be, to your other model's method(s).

Regardless, I can't see why it wouldn't be possible to include one model into another, per se. I don't think you can do it with the syntax you are showing, though. You would have to do it some other convoluted way. In any case, IMO, it's bad practice to include a model directly into another model.

KyleFarris
libraries cant use $this and $this->db
Click Upvote
In a library, you would use $this->ci->db as stated above
Jon Winstanley
Yea, but its ugly. Models are better
Click Upvote
+3  A: 

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/

MECU
+11  A: 
oribani
A: 

Ty vm Oribani!!!

Aldo
A: 

In situations like this in Code Igniter I prefer one of two posiibilities:

1) Have model's attribute and setter like this:

class X extends Model {
  var $Y_model;
  public function setY($Y) {
    $this->Y_model = $Y;
  }

  public function doItRightNow($a,$b) {
    $list = $this->Y_model->getSomeList($a,$b);
    // ...
  }
  // ...
}


And then use this setter before other methods to give an instance of other model so it can be used by methods.

$this->load->model('X');
$this->load->model('Y');
$this->X->setY($this->Y);
$this->X->doItRightNow($something,$somethingElse);



2) To have a parameter in method by which I will give an other model instance from controller.

class X extends Model {
  public function doItRightNow($a,$b,$Y_model) {
    $list = $Y_model->getSomeList($a,$b);
    // ...
  }
  // ...
}

And use it like this:

  $this->load->model('X');
  $this->load->model('Y');
  $this->X->doItRightNow($something,$somethingElse,$this->Y);



I think that these are more clean possibilities.
Which way to use depends on how many methods need to access other model. If there are one or two it might be better to give it as a method parameter. If more - I think it's better to have a class attribute and setter.
And in elegant way you can give one model or another depending on some condition - if they both partially implement same interface with the same kind of data returned (it's rarely useful, but it can be sometimes).

Zbyszek
A: 

Those are some VERY long answers for a simply question.

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

Phil Sturgeon