$this->params returns null in cakephp model

假如想象 提交于 2019-12-11 02:22:35

问题


I decided to add some extra data about the controllers and actions in some model beforeSave as follows:

//in the model
public function beforeSave() {
        $this->data[$this->alias]['path'] = 'blah blan';
        debug($this->params);
        die(); //for debugging!
}

The printout of debug returns null! The model I uses is the Comment model of the comments plugin. I need to access params to get the current controller, actions and some url parameters.

Indeed, I plan to change the way that comments plugin lists the comments from model based to be path based to solve the issue of need comments for more than one action depend on the same model.


回答1:


Finally I found the solution: It is in Router object method getParams();

//in the model
public function beforeSave() {
        $this->data[$this->alias]['path'] = 'blah blan';
        debug(Router::getParams());
        die(); //for debugging!
}

it prints out something like:

array(
    'plugin' => null,
    'controller' => 'qurans',
    'action' => 'view',
    'named' => array(
        'comment' => '0'
    ),
    'pass' => array(
        (int) 0 => '8'
    )
)


来源:https://stackoverflow.com/questions/19351515/this-params-returns-null-in-cakephp-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!