CakePHP - 将包含部分表数据的表单保存到数据库时出错

时间:2016-11-13 02:36:01

标签: php mysql forms cakephp

我在CakePHP 3中有一个新的用户表单,它收集用户的基本数据,例如用户名,电子邮件,姓名,电话等。这应该保存到Users表中,数据到表单中收集的数据,例如帐户是管理员,帐户创建的时间戳等等。所有这些其他字段都具有数据库中指定的默认值,例如0 {{1}在admin字段上,或false表示时间戳。我没有从表单中保存的其他人可以为空,没有默认值。

我遇到的问题是提交表单时,它会返回一般错误CURRENT_TIMESTAMP。我已使用The user could not be saved. Please, try again收集print_r数据以及request数据debug()$user看起来不错,而request } $user表示我希望数据库插入默认值的数据。

我做错了什么,为什么表单不会提交我给出的值,而数据库设置其余的默认值?

我的控制器:

This field is required

public function signup() { // set page-specific values $this->set('cakePage', false); if (!$this->request->session()->check('Session.timestamp')) { $this->redirect('/signup/home'); } $user = $this->Users->newEntity(); if ($this->request->is('post')) { $dob = implode("-", $this->request->data['birth_date']); $this->request->data['birth_date'] = date('Y-m-d', strtotime($dob)); $user = $this->Users->patchEntity($user, $this->request->data); if ($this->Users->save($user)) { $this->Flash->success(__('The user has been saved.')); return $this->redirect(['action' => 'index']); } else { $this->Flash->error(__('The user could not be saved. Please, try again.')); // debug print_r($this->request->data); echo "<br>"; print_r($user); } } $carriers = $this->Users->Carriers->find(); $this->set(compact('user', 'carriers')); $this->set('_serialize', ['user']); }

的结果
print_r($this->request->data);

Array ( [username] => asdf [password] => asdf [email] => asdf@gmail.com [first_name] => asdf [last_name] => asdf [birth_date] => 2000-01-01 [profile_image] => testUrl [bio] => Yo [phone] => 1234567890 [carrier] => 1 )

的结果
debug($user);

0 个答案:

没有答案