开发者

Pass variable in cakePHP

开发者 https://www.devze.com 2023-01-27 11:09 出处:网络
Im wondering how I can set some variables in my controller, and then be开发者_开发百科 able to access them in my model and a behaviour for that model.

Im wondering how I can set some variables in my controller, and then be开发者_开发百科 able to access them in my model and a behaviour for that model.

I have tried the below, but with no luck: in controller: $this->Model->data['foo']="bar"; in behaviour: $Model->data['foo'];

Grateful for any help!


One way to pass data is through user-defined functions in your model.

For example,

$flag = $this->Model->checkIntegrity($this->data);

In the Model,

function checkIntegrity($data) {
    ...
}

You could also pass them in as a reference if you wish to make direct changes to the data:

function checkIntegrity(&$data) {
    ...
}


It depends on what kind of data you're trying to pass around. If it's model/behavior settings you could use class properties. Example:

In model or behavior:

public $custom_variable = null; // or other default value

In controller:

$this->Model->custom_variable = 'new value';

or

$this->Behavior->custom_variable = 'new value';
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号