【问题标题】:how to save array data in database in cakephp3?如何在 cakephp3 的数据库中保存数组数据?
【发布时间】:2016-06-09 20:58:55
【问题描述】:

您好,我是 cakephp 新手,我想在数据库中保存一些数组数据 这是我的代码

 $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);

但是当我测试它时发生了这个错误: 调用数组上的成员函数 errors()。 我只想更改用户发布的数据并将其保存在用户表中。

【问题讨论】:

    标签: cakephp


    【解决方案1】:

    您已将$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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-27
      • 1970-01-01
      • 2012-05-28
      相关资源
      最近更新 更多