'guard' => false in new Entity() 选项在 Cakephp 4 中不起作用
'guard' => false in newEntity() options not working in Cakephp4
肯定是我做错了什么,但是我不能正确使用newEntity()
方法的guard
选项。
// Entity
class Bloc extends AbstractBloc
{
protected $_accessible = [
'*' => false // All fields are protected against mass assignment
];
}
'guard' => false
不允许在此示例中保存我的实体:
// Controller
public function test()
{
$entity = $this->Blocs->newEntity([
'titre' => 'TEST ASSIGNEMENT',
'rubrique_id' => 282,
'description' => 'Content'
], ['guard' => false]); // guard is false but nothing changes
if ($this->Blocs->save($entity)) {
return $this->redirect([...]);
}
else {
die('save is false');
}
}
我做错了什么?
Table::newEntity()
没有这个选项,guard
选项属于Entity::set()
。 newEntity()
的选项称为 accessibleFields
。
$entity = $this->Blocs->newEntity(
[
'titre' => 'TEST ASSIGNEMENT',
'rubrique_id' => 282,
'description' => 'Content',
],
[
'accessibleFields' => [
// '*' => true
'titre' => true,
'rubrique_id' => true,
'description' => true,
],
]
);
https://book.cakephp.org/4/en/orm/saving-data.html#changing-accessible-fields
肯定是我做错了什么,但是我不能正确使用newEntity()
方法的guard
选项。
// Entity
class Bloc extends AbstractBloc
{
protected $_accessible = [
'*' => false // All fields are protected against mass assignment
];
}
'guard' => false
不允许在此示例中保存我的实体:
// Controller
public function test()
{
$entity = $this->Blocs->newEntity([
'titre' => 'TEST ASSIGNEMENT',
'rubrique_id' => 282,
'description' => 'Content'
], ['guard' => false]); // guard is false but nothing changes
if ($this->Blocs->save($entity)) {
return $this->redirect([...]);
}
else {
die('save is false');
}
}
我做错了什么?
Table::newEntity()
没有这个选项,guard
选项属于Entity::set()
。 newEntity()
的选项称为 accessibleFields
。
$entity = $this->Blocs->newEntity(
[
'titre' => 'TEST ASSIGNEMENT',
'rubrique_id' => 282,
'description' => 'Content',
],
[
'accessibleFields' => [
// '*' => true
'titre' => true,
'rubrique_id' => true,
'description' => true,
],
]
);
https://book.cakephp.org/4/en/orm/saving-data.html#changing-accessible-fields