Cakephp 3使用array()附加$ this-> request->数据 - 间接修改重载属性数据无效

时间:2016-09-20 05:16:29

标签: cakephp cakephp-3.x

我一直在尝试和尝试很多,但我只是不明白如何实现这一点..这就是我想要实现的方式..

我正在使用cakephp 3.0,我试图通过在我的控制器中尝试以下代码来附加$this->request->data ...

$this->data['Leaveregisters'] = array(
                                'name'=>'another',
                                'surname'=>'whatever'
                                );
echo '<pre>';
print_r($this->request->data);
exit;

但我一直低于错误

  

注意(8):间接修改重载属性App \ Controller \ LeaveregistersController :: $ data无效

但是,我可以使用以下代码附加数据

$this->request->data['name'] = 'another';
$this->request->data['surname'] = 'whatever';

但我只是想知道为什么我无法使用array()实现同样的目标,或者我该怎么做才能实现这一目标?

如果我使用$this->request->data当前如果我使用下面的代码

,那么数组也应该放在同一个数组中
$this->request->data('Leaveregisters', [
    'name' => 'another',
    'surname' => 'whatever asdfa'
]);
echo '<pre>';
print_r($this->request->data);
exit;

它得到了上诉,但是下面有我不想要的新密钥

Array
(
    [leaveregister_user_id] => 2
    [leaveregister_num_leaves] => 15
    [leaveregister_sdate] => 2016-09-01
    [leaveregister_edate] => 2016-09-22
    [leaveregister_leave_type] => 1
    [leaveregister_status] => 1
    [leaveregister_created_date] => 2016-09-20 14:54:29
    [leaveregister_modified_date] => 2016-09-20 14:54:29
    [Leaveregisters] => Array
        (
            [name] => another
            [surname] => whatever asdfa
        )

)

我想要这样的东西......

  Array
    (
        [leaveregister_user_id] => 2
        [leaveregister_num_leaves] => 15
        [leaveregister_sdate] => 2016-09-01
        [leaveregister_edate] => 2016-09-22
        [leaveregister_leave_type] => 1
        [leaveregister_status] => 1
        [leaveregister_created_date] => 2016-09-20 14:54:29
        [leaveregister_modified_date] => 2016-09-20 14:54:29        
        [name] => another
        [surname] => whatever asdfa


    )

这样我就可以使用$this->request->data保存我的数据 有人可以指导我吗?

谢谢

1 个答案:

答案 0 :(得分:0)

CakePHP 3中没有Controller::$data属性。

<强> Cookbook > Appendices > 3.X Migration Guide > 3.0 Migration Guide > Controller

唯一正确的方法是访问请求对象,当然,如果你想以相同的方式设置嵌套数据,例如:

$this->request->data['Leaveregisters'] = [
    'name' => 'another',
    'surname' => 'whatever'
];

或者使用data()方法:

$this->request->data('Leaveregisters', [
    'name' => 'another',
    'surname' => 'whatever'
]);

另见