Welcome Guest, Not a member yet? Register   Sign In
Loading model from variable?
#1

[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
#2

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

InsiteFX
#3

[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);
#4

[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']);
#5

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

[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);
#7

[eluser]InsiteFX[/eluser]
Thanks wiredesignz,

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

InsiteFX
#8

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

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

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




Theme © iAndrew 2016 - Forum software by © MyBB