Welcome Guest, Not a member yet? Register   Sign In
Model's variables scope
#1

[eluser]Unknown[/eluser]
I'm a bit confused with Models in CI. Let's say I have a model with a variable declared like this:
Code:
var $variable
I also have 2 controller's functions.

The thing is, I want to initialize the model's variable in the first controller's function
and than use it in the second function. However, when I use the model in the second function, the variable is uninitialized.

It seems like when I use
Code:
$this->Model->...
I'm always reffering to a different object in a different function and I want to have only one object of the class Model.
#2

[eluser]WanWizard[/eluser]
In a standard CI setup, executing two controllers means two different page requests.

The web is stateless, one request is not aware of any other request. If you want to pass data from one request to the next, you have to use sessions.

If you mean multiple methods of a single controller, that should work. All CI classes are loaded as singletons, so
Code:
function func1()
{
    $this->load->model('testmodel');
    $this->testmodel->var = 'test';
}
function func2()
{
    echo $this->testmodel->var; // should echo 'test'
}
should work without problems.




Theme © iAndrew 2016 - Forum software by © MyBB