gpt4 book ai didi

php - CI4 您必须使用 "set"方法来更新条目。已经用set了,还是报错

转载 作者:行者123 更新时间:2023-12-04 08:41:39 29 4
gpt4 key购买 nike

我正在尝试使用要由 Codeigniter 4 框架更新的单个列来更新单个记录。已经 3 个小时了,但我仍然无法正常工作,文档和错误本身对我来说非常不清楚:You must use the "set" method to update an entry到目前为止,我试过这个:

public $books;
public $validation;

public function __construct()
{
$this->books = new Book();
$this->validation = Services::validation();
$this->validation->setRules([
'title' => 'string|max_length[100]',
'content' => 'string|min_length[20]|max_length[2000]'
]);
}

public function borrow($id)
{
if (!$this->books->find($id)) {
session()->setFlashData('error', 'No book.');
return redirect()->to('/books');
}

$data['borrower'] = $this->request->getVar('borrower');

// ERROR: You must use the "set" method to update an entry.
$this->books->where('id', $id);
$this->books->set($data);
$this->books->update('books');

// ERROR: You must use the "set" method to update an entry.
$this->books->find($id)
->update($id, $data);

// ALSO ERROR: You must use the "set" method to update an entry.
$this->books
->whereIn('id', [$id])
->set($data)
->update();

// AGAIN, ERROR: You must use the "set" method to update an entry.
$book = new Book();
$book->update($id, $data);

// STILL, ERROR: You must use the "set" method to update an entry.
$book->whereIn('id', [$id])
->set($data)
->update();

return redirect()->to('/books');
}
有什么线索吗?
编辑
我忘了添加 var_dump开始了:
var_dump($id);
var_dump($data);
die;

// RESULT: ['id'] => '3'
// RESULT: ['borrower'] => 'Jane doe'

最佳答案

我的天哪!刚刚意识到我没有指定 borrower protected $allowedFields 内的字段入口。

// Book model.
protected $allowedFields = [
'title',
'content',
'borrower' // <-- This thing was the problem, it isn't here before.
];

// Book controller.
// Now it works.
$this->books->update($id, $data);

关于php - CI4 您必须使用 "set"方法来更新条目。已经用set了,还是报错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64539005/

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