Cakephp保存用户数据失败,出现时间戳错误
我试图用下面的代码在控制器中保存我的表单数据,但失败了,错误消息说需要创建和更新。下面是我的代码,我还添加了UserTable模型类中的行为。我不知道这里遗漏了什么,请帮忙 保存用户数据的控制器代码Cakephp保存用户数据失败,出现时间戳错误,cakephp,cakephp-3.0,cakephp-model,Cakephp,Cakephp 3.0,Cakephp Model,我试图用下面的代码在控制器中保存我的表单数据,但失败了,错误消息说需要创建和更新。下面是我的代码,我还添加了UserTable模型类中的行为。我不知道这里遗漏了什么,请帮忙 保存用户数据的控制器代码 public function signup() { $user = $this->Users->newEntity(); if ($this->request->is('post')) { $user = $this->Users-&g
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中,我可以访问以下$\u
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'
]
我想我发现了问题所在。问题是验证器中的“requirerepresence”规则。由于该字段不是通过表单获得的(控制器将其设置为默认值),因此它不存在于传递的数组中 谢谢你的建议。。我试过了,但仍然是相同的错误,在更改UserTable中的内容后是否需要再次运行迁移。是的,您可以尝试。
**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',
]
]
]);
}