CodeIgniter Forums
Is it possible to UNLOAD a model? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Is it possible to UNLOAD a model? (/showthread.php?tid=4297)

Pages: 1 2


Is it possible to UNLOAD a model? - El Forum - 11-16-2007

[eluser]dcheslow[/eluser]
I am using:
Code:
$this->load->model('class_1','my_model',TRUE);
and access the model:
Code:
$this->my_model->some_function();
I later (in the same page load) use:
Code:
$this->load->model('class_2','my_model',TRUE);
When I execute:
Code:
$this->my_model->some_function();
I get the class_1 function, not the class_2 function, as I expected. If I change the name assigned to the model in the second load statement, then all is well... but that's inconvenient in my case because this code is being generated dynamically.
I tried unset($this->my_model) without success and I've searched the guide for anything about unloading... nothing there either.

Any suggestions?

Thanks,

=dave=


Is it possible to UNLOAD a model? - El Forum - 11-16-2007

[eluser]Alex007[/eluser]
Edit: I misread your post, sorry.


Is it possible to UNLOAD a model? - El Forum - 11-16-2007

[eluser]Pygon[/eluser]
You might re-consider how your dynamic code generation works, or consider using a variable reference, ie:
Code:
$this->load->module('class_1','my_model1',TRUE);
$myclass =& $this->my_model1;
$myclass->doSomething(); //same as $this->my_model1->doSomething()
$this->load->module('class_2','my_model2',TRUE);
unset($myclass);
$myclass =& $this->my_model2;
$myclass->doSomething(); //same as $this->my_model2->doSomething()

Another alternative would similar to the above:
Code:
$i = 1;
$this->load->module('class_'.$i,'my_model'.$i,TRUE);
$mymodule = 'this->my_model'.$i;
${$mymodule}->doSomething(); // same as $this->my_model1->doSomething()
$i++;
$this->load->module('class_'.$i,'my_model'.$i,TRUE);
$mymodule = 'this->my_model'.$i;
${$mymodule}->doSomething(); // same as $this->my_model2->doSomething()



Is it possible to UNLOAD a model? - El Forum - 11-17-2007

[eluser]bonza[/eluser]
You could try creating a new instance of the model. I have used the following to create unique XML_Parser instances. It would probably work equally well with models.

Code:
$this->load->library('xml_parser');
$xmlparser1 = new Xml_parser(file_get_contents($path1));
$xmlparser1->Parse();
$xmlparser2 = new Xml_parser(file_get_contents($path2));
$xmlparser2->Parse();



Is it possible to UNLOAD a model? - El Forum - 04-01-2009

[eluser]Nameless One[/eluser]
Sorry for bumping an old topic, but I have a solution which might help people with similar problems. I had problem with loading modules in foreach loop. The cause for this is that $this->load->model() registers the model it loads into a member variable under the assigned variable name. Even if you do unset($this->variable_name), the model will become unusable but still registered as loaded into $this->variable_name.

The solution is:
Code:
foreach ($some_array as $model_name)
{
    $this->load->model($model_name,'some_variable');
    $results[$model_name] = $this->some_variable->some_function();
    unset($this->some_variable);
    unset($this->load->_ci_models[array_search('some_variable',$this->load->_ci_models)]);
}

Not very elegant, but it is the olny way unless the CI developers decide to kindly provide us with unload() method.


Is it possible to UNLOAD a model? - El Forum - 10-17-2009

[eluser]tunesmith[/eluser]
Hi, can't these problems be solved by just instantiating?

Code:
$this->load->model('User');
$user = new User;
// blah blah
unset($user);
$user = new User;
// etc...



Is it possible to UNLOAD a model? - El Forum - 10-18-2009

[eluser]Nameless One[/eluser]
[quote author="tunesmith" date="1255860153"]Hi, can't these problems be solved by just instantiating?

Code:
$this->load->model('User');
$user = new User;
// blah blah
unset($user);
$user = new User;
// etc...
[/quote]

Haven't tried it but I think it would work. It's not really optimized, though, since $this->load->model() already instantiates a model in $this->User so you would basically have an unused object in $this->User at all times.


Is it possible to UNLOAD a model? - El Forum - 02-17-2012

[eluser]jwright[/eluser]
[quote author="Nameless One" date="1238605268"]Sorry for bumping an old topic, but I have a solution which might help people with similar problems. I had problem with loading modules in foreach loop. The cause for this is that $this->load->model() registers the model it loads into a member variable under the assigned variable name. Even if you do unset($this->variable_name), the model will become unusable but still registered as loaded into $this->variable_name.

The solution is:
Code:
foreach ($some_array as $model_name)
{
    $this->load->model($model_name,'some_variable');
    $results[$model_name] = $this->some_variable->some_function();
    unset($this->some_variable);
    unset($this->load->_ci_models[array_search('some_variable',$this->load->_ci_models)]);
}

Not very elegant, but it is the olny way unless the CI developers decide to kindly provide us with unload() method.[/quote]


This method didn't work for me. The cleanest way I found to achieve the post authors goal is this...

Code:
$this->load->model('class_1', 'my_model');

$this->my_model->some_class_1_function();

$this->load->model('class_2', 'my_model_2');

$this->my_model = $this->my_model_2;

$this->my_model->some_class_2_function();

The above should work. (notice the second to last line of code)

I also believe that the concept of unloading is either impossible or very difficult to do. This is because once the actual class in 'class_1' is loaded in php, there is no way to 'unload' it (at least not that I'm aware of, but you could search the php Docs).

But basically, just setting the 'my_model' variable again yourself, should do the trick. It worked for me.



Is it possible to UNLOAD a model? - El Forum - 02-18-2012

[eluser]Aken[/eluser]
There really is no efficient way to replace a model name that's attached to the CI super object currently built into CodeIgniter. In response to the original post (even though it's old) - if you have two different models with two different functions, I don't see why you'd want to use the same alias for those separate models anyway. I think it's a good way to confuse yourself (and maybe others) down the road.

Personally, instantiating is the best way to handle either multiple instances of the same model, or the replacement of one model for the next in the same variable. Then you don't have to argue with CI's loader class and its settings, and you can make use of unset() and whatnot.

As for an unload() method on the loader, that'd be the opposite of impossible - quite easy actually. You could consider that an option for your application if you felt it was necessary. Extending the loader class is easy. The methods behind it wouldn't unload the class from PHP, it would basically run unset() on CI variables to remove the model like it was never there in the first place. Pretty much the same thing as what Nameless One posted.


Is it possible to UNLOAD a model? - El Forum - 02-20-2012

[eluser]jwright[/eluser]
Hi Aken,

You can see the reason I needed to replace a model here in the constructor of this class https://github.com/wrightlabs/tank_auth_groups/blob/master/application/libraries/Tank_auth_groups.php . The class I replaced the model with, inherited from the class of the original model object. In most normal circumstances, you're right, there would be no reason to replace a model object once loaded. In this case, I had the goal of not touching any existing code, so inheritance and replacing the model object helped me achieve this goal.

As far as unloading, my main concern is not with CodeIgniter. But once you load a php file with "require" (or similar), if that php file has a class declaration in it, I'm not sure how to "unload" that class declaration from that specific run of the php interpreter (for that page view). While it might be possible (if it is I'm sure it's in the php docs somewhere), I'm not aware how to do it.
What made me aware of this issue is an error you will get that says something like "cannot re-declare class 'My_model'" . This can happen if a file (containing a class declaration) has already been loaded and unloaded from CodeIgniter, and you try to load it again or a class with the same name. So unloading can be more tricky than it sounds. I imagine this is why there is no official unload method in CodeIgniter itself.

UPDATE: After some searching, I wasn't able to find anything in the PHP Docs concerning unloading classes. But I did see a few responses on stackoverflow.com that stated "It's not possible to unload a class from PHP". So from the best I can tell, it's not possible. If anyone finds a way to unload a class from PHP (which CodeIgniter is built on), I'd be interested to know.