CodeIgniter Forums
Model singleton - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: Model singleton (/thread-19017.html)



Model singleton - El Forum - 05-25-2009

[eluser]ClaudioX[/eluser]
Hi all,

I'm reading this document conceptric.co.uk/, and in some point of the text he said:
Quote:CodeIgniter Models implement the Singleton design pattern
, now i have a doubt, when i'm load one model, the instance (and properties) are sharing across the application?


Thanks


Model singleton - El Forum - 05-25-2009

[eluser]Jondolar[/eluser]
You load a model like this:
$this->load->model('Model_name');

You access it like this:
$this->Model_name->function();

Not really an opportunity to load and access more than one instance. There may be a way around this but I haven't investigates.


Model singleton - El Forum - 05-25-2009

[eluser]Dam1an[/eluser]
Without reading the article, are you concerned that the variables in my instance of a model will overwrite yours?
Cause if thats your concern I'm pretty sure thats not the case
Each HTTP reqiuest gets its own instance of CI, so that instance of a model is just yours


Model singleton - El Forum - 05-25-2009

[eluser]ClaudioX[/eluser]
Thanks for the reply,

jondolar, i understand that to.

Damian, I was concerned about the issue you raised,
and you have completely solved, ty man!

but the original matter is: i think in aplication when i use more than one
model, and more than one time, if i have,

Code:
$this->load->model("pilot");
$this->load->model("race");

$this->pilot->setName("JJ");

$this->race->start();

//in the race model..
function start(){

$this->load->model("pilot");
$this->pilot->setName("BB king");

}

$this->pilot->save(); //is now BB king right?

In the fw i have worked, the the pilot will have two instaces, in CI I think not,
its thats a Domain question?

UPDATE:

Studing a way, i found ellislab.com/forums/viewthread/49625/#247644

now i think in the submodels, use this form to do a new load:

Code:
$this->load->mode('a_object');
$myObj = new a_object();

What do you think about? thats right?


Model singleton - El Forum - 05-25-2009

[eluser]Dam1an[/eluser]
You can always alias a model using the second parameter
Code:
$this->load->model('pilot', 'pilot1');
$this->pilot1->setname("Pilot 1");

$this->load->model('pilot', 'pilot2');
$this->pilot2->setname("Pilot 2");