Cakephp 保存用户数据失败,出现时间戳错误
Cakephp Saving User Data is failing with timestamp error
我试图在控制器中使用以下代码保存我的表单数据,但它失败了,并显示错误消息,指出需要创建和更新。下面是我的代码,我还在 UserTable 模型 class 中添加了行为。不知道这里遗漏了什么,请大家帮忙
保存用户数据的控制器代码
public function signup()
{
$user = $this->Users->newEntity();
if ($this->request->is('post')) {
$user = $this->Users->patchEntity($user, $this->request->getData());
if ($this->Users->save($user)) {
$this->Flash->success(__('The user has been saved.'));
return $this->redirect(['action' => 'index']);
}
debug($user->errors());
$this->Flash->error(__('The user could not be saved. Please, try again.'));
}
$this->set(compact('user'));
$this->set('_serialize',['user']);
}
在 user.php 我有以下 $_accessible
protected $_accessible = [
'display_name' => true,
'email' => true,
'mobile' => true,
'password' => true,
'created_at' => true,
'updated_at' => true
];
并且在 UserTable 中我也添加了行为
public function initialize(array $config)
{
parent::initialize($config);
$this->setTable('users');
$this->setDisplayField('id');
$this->setPrimaryKey('id');
$this->addBehavior('Timestamp');
}
保存数据时出现以下错误。
[
'created_at' => [
'_required' => 'This field is required'
],
'updated_at' => [
'_required' => 'This field is required'
]
**In UserTable you can try behavior like this:**
public function initialize(array $config)
{
parent::initialize($config);
$this->setTable('users');
$this->setDisplayField('id');
$this->setPrimaryKey('id');
$this->addBehavior('Timestamp', [
'User' => [
'Model.beforeSave' => [
'created_at' => 'new',
'updated_at' => 'always',
]
]
]);
}
我想我找到了问题所在。问题是验证器中的 "requirePresence" 规则。由于该字段不是通过表单获得的(它由控制器设置为默认值),因此它不存在于传递的数组中。
我试图在控制器中使用以下代码保存我的表单数据,但它失败了,并显示错误消息,指出需要创建和更新。下面是我的代码,我还在 UserTable 模型 class 中添加了行为。不知道这里遗漏了什么,请大家帮忙
保存用户数据的控制器代码
public function signup()
{
$user = $this->Users->newEntity();
if ($this->request->is('post')) {
$user = $this->Users->patchEntity($user, $this->request->getData());
if ($this->Users->save($user)) {
$this->Flash->success(__('The user has been saved.'));
return $this->redirect(['action' => 'index']);
}
debug($user->errors());
$this->Flash->error(__('The user could not be saved. Please, try again.'));
}
$this->set(compact('user'));
$this->set('_serialize',['user']);
}
在 user.php 我有以下 $_accessible
protected $_accessible = [
'display_name' => true,
'email' => true,
'mobile' => true,
'password' => true,
'created_at' => true,
'updated_at' => true
];
并且在 UserTable 中我也添加了行为
public function initialize(array $config)
{
parent::initialize($config);
$this->setTable('users');
$this->setDisplayField('id');
$this->setPrimaryKey('id');
$this->addBehavior('Timestamp');
}
保存数据时出现以下错误。
[
'created_at' => [
'_required' => 'This field is required'
],
'updated_at' => [
'_required' => 'This field is required'
]
**In UserTable you can try behavior like this:**
public function initialize(array $config)
{
parent::initialize($config);
$this->setTable('users');
$this->setDisplayField('id');
$this->setPrimaryKey('id');
$this->addBehavior('Timestamp', [
'User' => [
'Model.beforeSave' => [
'created_at' => 'new',
'updated_at' => 'always',
]
]
]);
}
我想我找到了问题所在。问题是验证器中的 "requirePresence" 规则。由于该字段不是通过表单获得的(它由控制器设置为默认值),因此它不存在于传递的数组中。