CakePHP 3.7:函数名必须是字符串

席蒙·加文斯卡

我在 UsersController 中有一个方法

    public function addMailbox($data)
    {
              $this->LoadModel('Mailbox');
              $mailbox = $this->Mailbox->newEntity();
              $mailbox->username = $data('username');
              $mailbox->name = $data('name');

        if ($this->Mailbox->save($mailbox)) {
            return $this->redirect(['action' => 'index']);
            }
        $this->Flash->error(__('Error'));
       }

,代码在粘贴到 add() 方法时工作正常,但在使用后

     $this->addMailbox($this->request->getData());

我得到的只是错误:函数名称必须是字符串

有任何想法吗?

阿霍夫纳

在 PHP 中访问数组的语法错误,请使用方括号:

$mailbox->username = $data['username'];
$mailbox->name = $data['name'];

按照您的方式,它尝试使用名为 in 的变量调用函数$data,但 $data 是一个数组而不是字符串(有关更多信息,请参阅变量函数)。

此外,您不应直接在 $mailbox 属性上设置用户输入 - 这会绕过验证。相反,只需将 $data 插入newEntity()

public function addMailbox($data)
{
    $this->loadModel('Mailbox'); // This also is not required if this function is inside the MailboxController
    $mailbox = $this->Mailbox->newEntity($data);

    if ($this->Mailbox->save($mailbox)) {
        return $this->redirect(['action' => 'index']);
    }
    $this->Flash->error(__('Error'));
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章