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

#11
[eluser]Aken[/eluser]
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.

#12
[eluser]autefrum[/eluser]
I too have this problem.

I have code:
Code:
foreach($id_array as $id) {
   $model_name="model_".$id;
   $this->load->model($model_name,"model_n");  
   $ret_val[$id]=$this->model_n->get_something();
}

So I tried to overcome using:
Code:
unset($this->model_n);
unset($this->load->_ci_models[array_search('model_n',$this->load->_ci_models)]);

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:
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();

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

So I am a little stuck.....

Has anyone overcome this in the last few years?

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

This does not work.

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

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.

#14
[eluser]autefrum[/eluser]
SOLVED!

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:

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

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

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.