CodeIgniter Forums
Model singleton - Printable Version

+- CodeIgniter Forums (
+-- Forum: Archived Discussions (
+--- Forum: Archived General Discussion (
+--- Thread: Model singleton (/thread-19017.html)

Model singleton - El Forum - 05-25-2009

Hi all,

I'm reading this document, 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?


Model singleton - El Forum - 05-25-2009

You load a model like this:

You access it like this:

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

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

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,




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

$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?


Studing a way, i found

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

$myObj = new a_object();

What do you think about? thats right?

Model singleton - El Forum - 05-25-2009

You can always alias a model using the second parameter
$this->load->model('pilot', 'pilot1');
$this->pilot1->setname("Pilot 1");

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