Welcome Guest, Not a member yet? Register   Sign In
Model singleton
#1

[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
#2

[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.
#3

[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
#4

[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?
#5

[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");




Theme © iAndrew 2016 - Forum software by © MyBB