Welcome Guest, Not a member yet? Register   Sign In
[Fixed] Call model dynamically
#1

[eluser]louis w[/eluser]
Trying to call a model dynamically.

Code:
$this->load->model($type.'_model');
$this->$type.'_model'->get();

I know the code on the second line is invalid. Do you know of a way to do this? I do not want to set an alternative name for the model when loading because this will be in a loop and I can foresee conflicts.

Thanks.


Update
The fix was

Code:
$this->{$type.'_model'}->get();
#2

[eluser]Randy Casburn[/eluser]
Hi,

Glad you got it sorted!

[quote author="louis w" date="1219458334"]Trying to call a model dynamically.

Code:
$this->load->model($type.'_model');
$this->$type.'_model'->get();

I know the code on the second line is invalid. Do you know of a way to do this? I do not want to set an alternative name for the model when loading because this will be in a loop and I can foresee conflicts.

Thanks.


Update
The fix was

Code:
$this->{$type.'_model'}->get();
[/quote]


It's more conventional to do your concatenation outside your object references thus:

Code:
$fred = $type.'_model';

$this->load->model($fred);
$this->$fred->get();

codes a little cleaner that way and it would have worked from the start without the special operators.

Either way though.

Randy
#3

[eluser]louis w[/eluser]
Thanks Randy. For some reason i thought i tried this. Your right - much cleaner.




Theme © iAndrew 2016 - Forum software by © MyBB