如何在cakephp3的数据库中保存数组数据?
问题描述:
嗨,我是在CakePHP中新,我想保存在数据库 这里有些阵列中的数据是我的代码如何在cakephp3的数据库中保存数组数据?
$user = $this->Users->newEntity();
if ($this->request->is('post')) {
$user = $this->Users->patchEntity($user, $this->request->data);
$userdata=[
'firstname'=>$user['firstname'],
'lastname'=>$user['lastname'],
'nationcod'=>$user['nationcod'],
'usrename'=>$user['username'],
'password'=>$user['username']
];
$this->Users->save($userdata);
,但是当我测试了这个错误发生: 调用一个成员函数错误()上阵列。 我只想更改用户发布的数据并将其保存在用户表中。
答
您已将$user
分配给$this->Users->newEntity();
。并且你正在使用$user['firstname']
。它不会有任何内容,因为它是一个不是数组的实体。从你的问题,我想你想保持用户的用户名作为他们的密码。所以你所要做的就是这个。
$user=$this->Users->newEntity();
if($this->request->is('post')){
$this->request->data['password']=$this->request->data['username'];
$user=$this->Users->patchEntity($user,$this->request->data);
$this->Users->save($user);
}