CI4 您必须使用 "set" 方法来更新条目。已经用set了,还是报错
CI4 You must use the "set" method to update an entry. Already use set, still error
我正在尝试使用要由 Codeigniter 4 Framework 更新的单个列来更新单个记录。已经 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'
我的天哪!刚刚意识到我没有在 protected $allowedFields
条目中指定 borrower
字段。
// 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);
我正在尝试使用要由 Codeigniter 4 Framework 更新的单个列来更新单个记录。已经 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'
我的天哪!刚刚意识到我没有在 protected $allowedFields
条目中指定 borrower
字段。
// 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);