gpt4 book ai didi

cakephp - 错误 : Call to a member function newEntity() on boolean

转载 作者:行者123 更新时间:2023-12-02 08:10:41 25 4
gpt4 key购买 nike

我是 cakephp 的新手。我已经完成了所有必需的步骤,但仍然无法使用 cakephp 将数据保存到数据库中

Articlecontroller.php 中的 adduser 函数代码:

public function adduser()
{
$user = $this->Users->newEntity();
if ($this->request->is('post')) {
$user = $this->Users->patchEntity($user, $this->request->getData());

// Hardcoding the user_id is temporary, and will be removed later
// when we build authentication out.
$user->user_id = 1;

if ($this->Users->save($user)) {
$this->Flash->success(__('Your article has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('Unable to add your article.'));
}
$this->set('article', $user);
}

UserTable模型代码:

<?php
// src/Model/Table/ArticlesTable.php
namespace App\Model\Table;

use Cake\ORM\Table;

class UsersTable extends Table
{
public function initialize(array $config)
{
$this->addBehavior('Timestamp');
}
}

到我的本地主机上的数据库表: enter image description here

最佳答案

我想你忘了在 Controller 中加载用户模型。在第一行之前在函数 adduser() 中添加这一行应该是固定的。它应该看起来像这样。

public function adduser()
{
$this->loadModel('Users');
$user = $this->Users->newEntity();
...

Cakephp 文档。 https://book.cakephp.org/3.0/en/controllers.html#loading-additional-models

关于cakephp - 错误 : Call to a member function newEntity() on boolean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47373505/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com