• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is it possible to UNLOAD a model?

Unloading a class from CodeIgniter and removing or re-declaring a class name from the PHP scope are two different things. So yes, we're both right, cause we're talking about different things.

I too have this problem.

I have code:
foreach($id_array as $id) {

So I tried to overcome using:

but I get an error:
Cannot access protected property CI_Loader::$_ci_models in {local path}\application\models\account_model.php

I don't want to stuff around with the core and change the _ci_models to be a public property, but I support I could....

And I can't use this:
$this->load->model('class_1', 'my_model');


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

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


As I don't want to hard code the 2 in
$this->my_model = $this->my_model_2;

So I am a little stuck.....

Has anyone overcome this in the last few years?

[quote author="Pygon" date="1195242298"]You might re-consider how your dynamic code generation works, or consider using a variable reference, ie:

This does not work.

I fixed up the type "module" became model and run:
$i = 1;
  $mymodule = 'this->model'.$i;
  echo "mymodule:$mymodule<br>";
  echo ${$mymodule}->doSomething();
  $mymodule = 'this->model'.$i;

but got "Message: Undefined variable: this->model1" on the line with ${$mymodule} in it. I guess it is looking for a variable inside the {} rather than an object and property/function.


Persistence pays off, and with the help of the others in this forum, here is how I fixed it.

I used variable references as suggested by Pygon, but not for the entire "$this->model", just for the model name:

for ($i = 1;$i<$number_of_mdels;$i++) {
$model_name = 'model_'.$i;
echo "model_name:$model_name<br>";
echo $this->$model_name->return_a_string()."<br>";

Note the
$this->$model_name (where $model_name=="model_1")
rather than

I guess this is more of a php language query/solution than a codeIgniter query/solution but it had me stumped for a few weeks...

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.