CodeIgniter Forums
Cannot pass variables from Controller to Model - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Development (https://forum.codeigniter.com/forumdisplay.php?fid=6)
+--- Forum: Issues (https://forum.codeigniter.com/forumdisplay.php?fid=19)
+--- Thread: Cannot pass variables from Controller to Model (/showthread.php?tid=65319)



Cannot pass variables from Controller to Model - allenlee - 05-29-2016

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.



Test

[Image: obY1dVD.jpg]

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

[Image: Ko2oreo.jpg]

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

It works.
[Image: IxrB8QB.jpg]

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

Error
[Image: BjldjTS.jpg]


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


How to solve this issue?


RE: Cannot pass variables from Controller to Model - Tpojka - 05-29-2016

You are not extending core files with your controller|model at all.
Shouldn't
PHP Code:
core/Model.php 
be
PHP Code:
core/MY_Model.php 
?


RE: Cannot pass variables from Controller to Model - skunkbad - 05-29-2016

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:

Code:
$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:

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



RE: Cannot pass variables from Controller to Model - ciadmin - 05-29-2016

$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.


RE: Cannot pass variables from Controller to Model - PaulD - 05-29-2016

Hi,

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.

Paul.

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


RE: Cannot pass variables from Controller to Model - allenlee - 05-29-2016

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.


RE: Cannot pass variables from Controller to Model - albertleao - 05-29-2016

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.


RE: Cannot pass variables from Controller to Model - ivantcholakov - 05-29-2016

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;

[]


RE: Cannot pass variables from Controller to Model - Narf - 05-30-2016

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


RE: Cannot pass variables from Controller to Model - spjonez - 06-01-2016

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