Welcome Guest, Not a member yet? Register   Sign In
Cannot pass variables from Controller to Model

Is it a bug in CodeIgniter 3 ?

It is very strange. If I set a variable in MY_Controller, any model can receive this value.

But if I set a variable in normal Controller, any model can not. receive this value.

I believe it is a bug because CodeIgniter 2.x works good, but CodeIgniter 3 doesn't.


[Image: obY1dVD.jpg]

I set a variable in MY_Controller 
$this->test_message = 'this is a test';

[Image: Ko2oreo.jpg]

Echo this variable in Model
echo 'ttt: ' . $this->test_message;

It works.
[Image: IxrB8QB.jpg]

But if I set it in  normal Controller
[Image: JZoCqT4.jpg]

[Image: BjldjTS.jpg]

I found this issue from upgrade my old website from Codeigniter 2 to 3.

How to solve this issue?
Personal blog: https://terryl.in
My personal project is called Dictpedia is currently using Codeigniter 3, welcome.

You are not extending core files with your controller|model at all.
PHP Code:
PHP Code:

Whether this works or not, this is just a bad coding habit. Without comments, there's no way to show where the variable was set (in your model), or what you're doing with it (in your controller).

In your controller, you should do this instead:

$this->news_model->test_message = 'this is a test';

Even better would be to pass the message from the controller to the model in a method call:

// Controller
$message = 'this is a test';
$this->news_model->set_message( $message );
// Model
public function set_message( $message )
  $this->test_message = $message;

$this refers to the current object context.

Inside your model, $this->test_message should refer to a model property, not a controller one.

If you want to reference controller properties inside a model (not that this is a good idea), you would use something like

$CI = &get_instance();
... $CI->test_message

Without seeing more of your project, I cannot guess at why your code might have worked as described in CI2.

Your problem is *not* a bug ... CI was not intended to be used the way you have described.

(This post was last modified: 05-29-2016, 09:33 AM by PaulD. Edit Reason: Added PS )


I thought that models and controllers were all part of the same context. A site I am building right now, I decided to set user_id as in the controller construct so I can access it in any called models and controller methods without having to fetch it all the time from the user_model nor pass it every model call.

It is working fine, but are you saying that this is a bad idea? Or that is should not be working?

I do not use it to send any other info like messages or other variables, but user_id I am referring to in every database call and every method, so I thought it was a handy approach that I had not used before.


PS Or is it because the OP was not extending the CI_controller that it was a problem?

The code I put here is just a test.. not my site source code. (Clean install CI 3.06 and CI 2 to simply test it )
Just let you know it works in CI 2 (works in any controller), but CI 3 doesn't (only works in MY_Controller).

The strange thing is, it works only set variables in MY_Controller.

If it is bad coding habit, CI 3 should deprecate it, not allow only set variables in MY_Controller, it makes confusion.
Personal blog: https://terryl.in
My personal project is called Dictpedia is currently using Codeigniter 3, welcome.

Setting a user_id variable available to the model is bad practice. The goal is to have as much modularity as possible and tying your models to a variable set in your MY_Controller will make it dependent.
Codeigniter is simply one of the tools you need to learn to be a successful developer. Always add more tools to your coding arsenal!

Public controller properties can be accessed within model context in the way $this->test_message , "magic" is used for this. But I don't use this feature for passing values, it is handy for accessing objects that the controller contains. Within a model you can write $value = $this->session->userdata('value'); instead of $CI = &get_instance(); $value = $CI->session->userdata('value');

Anyway, if you want to pass to the controller a property value (don't like this), better declare it within the corresponding controller public $test_message;


You're setting the property after the model is loaded; it doesn't yet exist at the time you're trying to access it.

In traditional MVC controllers and models should not cross talk. Personally I wouldn't build an application this way.

Theme © iAndrew 2016 - Forum software by © MyBB