CodeIgniter Forums
Loading model from variable? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: Loading model from variable? (/showthread.php?tid=40701)



Loading model from variable? - El Forum - 04-15-2011

[eluser]debow[/eluser]
Is there a way to load a model from variable and call it throughout the controller.

IE:

controller/??.php

Code:
var $data = array(
        'title' => 'test',
    'model' => 'My_Model',
    );


$this->load->model('$model') //This doesnt work...is there a way around it.

The reason being is I have controllers I can copy/paste and the only different is the table they are calling and their name so instead of having to change the the model name thats used many times in the controller I want to just change it in one location on each controller.

Also want to call the model in this way as well.

Code:
$results = $this->$model->search($limit, $offset, $sort_by, $sort_order);

Thanks


Loading model from variable? - El Forum - 04-15-2011

[eluser]InsiteFX[/eluser]
Code:
$this->load->model($model) //This doesnt work...is there a way around it.

InsiteFX


Loading model from variable? - El Forum - 04-16-2011

[eluser]toopay[/eluser]
Based by your ilustration...
Code:
var $data = array(
               'title' => 'test',
               'model' => 'My_Model',
            );

$this->load->model($data['model']);

// This will work, but you should load it first, as line above, except you already put it in autoload
$results = $this->$data['model']->search($limit, $offset, $sort_by, $sort_order);



Loading model from variable? - El Forum - 04-16-2011

[eluser]wiredesignz[/eluser]
@InsiteFX and toopay: The array is obviously a class variable.
Code:
var $data = array(
       'title' => 'test',
       'model' => 'My_Model',
    );

$this->load->model($this->data['model']);



Loading model from variable? - El Forum - 04-16-2011

[eluser]ηυмвєяσηє[/eluser]
btw, try "$model" instead of '$model'.


Loading model from variable? - El Forum - 04-16-2011

[eluser]toopay[/eluser]
@wiredesignz

You're right about class var, i think both insiteFX and i didn't aware of that, especially for someone which reply this thread while drink a beer(thats me). It should be '$this->varname' not '$varname'.

But he did clearly put this request too..
[quote author="debow" date="1302943535"]
...
Also want to call the model in this way as well.

Code:
$results = $this->$model->search($limit, $offset, $sort_by, $sort_order);
[/quote]

and then now, after clearing about class var, it seems you slightly slipped too, in your solution logics...
[quote author="wiredesignz" date="1302959405"]@InsiteFX and toopay: The array is obviously a class variable.
Code:
var $data = array(
       'title' => 'test',
       'model' => 'My_Model',
    );

$this->load->model($this->data['model']);
[/quote]
based by above lines, then it will be running into this way...
Code:
$this->load->model($this->data['model']);
$results = $this->$this->data['model']->search($limit, $offset, $sort_by, $sort_order);
Last above line, will triggering some 'A PHP Error was encountered...bla..bla'.

@debow
Code:
//...some class var....
var $data = array(
               'title' => 'test',
               'model' => 'My_Model',
            );

//...some function within controller...
$local_var = $this->$data['model'];
$this->load->model($local_var);
$results = $this->$local_var->search($limit, $offset, $sort_by, $sort_order);



Loading model from variable? - El Forum - 04-16-2011

[eluser]InsiteFX[/eluser]
Thanks wiredesignz,

I had a wicked head ache last night! Should have gone to bed LOL!

InsiteFX


Loading model from variable? - El Forum - 04-16-2011

[eluser]toopay[/eluser]
[quote author="InsiteFX" date="1302983974"]...Should have gone to bed LOL!
InsiteFX[/quote]

@InsiteFX, yep.. Your eyes seems tired... Tongue


Loading model from variable? - El Forum - 04-16-2011

[eluser]debow[/eluser]
Thanks toopay, that worked and its much eaiser to reuse my controllers from one to the other.