Welcome Guest, Not a member yet? Register   Sign In
Loading multiple instances of another model in a model
#1

[eluser]Unknown[/eluser]
I've been developing with CI for a few weeks now, but I'm running into some problems right now.

Usually I do this:
Code:
//Model Page
function getAllObjects()
{
    $objects = {Query that fetches queries from my db};
    $results = array();
    foreach($objects as $r)
    {
        $o = new Object(); //Another model of mine
            $o->getObjectUsed($r->id); //Get's the object type and content that is saved.
            $results[] = $o;
    }
    return $results;
}
To explain what I'm getting at: I have a page with several area's that have objects assigned to them.

This unfortunately doesn't work. If I load the objects only the last object declared will be shown. It will be shown the X amount of times I get a result from the db. That's what CI does apparently and that is good, for the entire other part of my application anyway. But isn't there a way to deactivate that variable exchanging?
I mean how not useful is it to only be able to load one instance of the object class?

CI wise would be this I guess:
Code:
$this->load->model("Object","bla");
$this->bla->getObjectUsed($r->id);
$results[] = $this->bla;
But then $this->bla is not declared, I'm guessing because declaring a model load in model doesn't work.

Have I overlooked something? I read the entire user guide but that only helps so little when making the actual thing.
Help! It's driving me crazy...
#2

[eluser]CtheB[/eluser]
You should not load a model inside a model.

To do it like CI you'll need to handle things in your controller:

Basic_Objects_Controller.php
Code:
public function getObjects()
{
     $this->load->model("Objects", "object", TRUE);
  
     $arObjects = $this->object->getAllObjects();

      $this->load->model("otherObjects", "otherobject", TRUE);

      $results = array();
    
      foreach($arObjects as $r)
      {
            if($o = $this->otherobject->getObjectUsed($r->id))
            {
                   $results[] = $o;
            }
      }
      return $results;
}

In the models you only handle the queries. In the controller, you can interact with different kind of models.
Good luck and if you have any more questions don't hesitate to ask.
#3

[eluser]taviroquai[/eluser]
Hi!

I'm creating models and instanciating models inside models all over my application and it seems ok...

Could you please be more detailed in explaining why one should not load a model, and instantiate, inside another model?

Thanks
#4

[eluser]taviroquai[/eluser]
Ahh... after reading a while on Design Patterns and Singleton... i now understant that when a model is loaded by a controller, by default automatically is created an instance of the Model. A model is, by default imposition, always a singleton.

I think that a model should be treated like a normal Object that can be instanciated more than once. Ok, ok... I know that we can create more than one instance of a model using $this->load->model('path', 'Name') but that is not a normal way to instantiate objects on an OOP way.




Theme © iAndrew 2016 - Forum software by © MyBB