CakePHP 无法插入数据库(日期时间格式)
CakePHP Unable to insert to database (datetime format)
我是 Cakephp 的新手,我实际上遵循了博客教程的教程,但是我没有遵循他们的数据库并尝试自己的。
目前我的数据库:
我的模特:
class FypCakephp extends AppModel {
//Table Name
public $useTable = 'Report';
public $primaryKey = 'report_id';}
我的控制器:
public function add() {
if ($this->request->is('POST')) { //Submited the form:takes a single argument, which can be the request METHOD (get, put, post, delete) or some request identifier (ajax).
$this->FypCakephp->create();// Create a model
// can use the pr() or debug() functions to print it out if you want to see what it looks like.
echo pr(($this->request->data));
debug($_POST);
if ($this->FypCakephp->save($this->request->data)) { //Save to Database and return true.
$this->Session->setFlash(__('Your post has been saved.'));
// return $this->redirect(array('action' => 'index')); //Redirect to the first page
}
$this->Session->setFlash(__('Unable to add your Database.')); //Else will show unable to add
}
}
我的看法:
http://gyazo.com/a69dd4cdfcf008b76d5c3dc5392d2009
每次我调试他们都会给我看这个
http://gyazo.com/7f8f43cbd88aee555aeb6690f33093e0
他们永远接受了 0 个查询。
当我将日期时间更改为文本类型时。他们也只能插入 created 和 modified 。
请问是什么原因造成的?
@初学者。我看到了上面的代码。从cakephp的形式来看,我认为它的结构不太好。初学者阶段请先阅读CakePHP 2.x documentation。然后尝试开发一个基本的 crud 应用程序。
我给你一个postlink。
Creating a CakePHP CRUD Example – Source Code Download and Tutorial
更新 - 1
根据您的代码将 datetime
保存到数据库
public function add() {
if ($this->request->is('POST')) {
//Submited the form:takes a single argument, which can be the request METHOD (get, put, post, delete) or some request identifier (ajax).
$this->FypCakephp->create();// Create a model
$data = $this->request->data;
$updated_date = date('Y-m-d H:m:i', strtotime($data['FypCakephp']['updated_date']));
$data['FypCakephp']['updated_date'] = $updated_date;
// can use the pr() or debug() functions to print it out if you want to see what it looks like.
echo pr($data);
debug($_POST);
if ($this->FypCakephp->save($data)) { //Save to Database and return true.
$this->Session->setFlash(__('Your post has been saved.'));
// return $this->redirect(array('action' => 'index')); //Redirect to the first page
}
$this->Session->setFlash(__('Unable to add your Database.')); //Else will show unable to add
}
}
您的问题正在查看:)
将报告更改为报告。
现在,当您保存 $this->request->data 时,它会尝试查找 Report key 以便能够在您的数据库中保存或报告字段。
如果你不想改变这个,在控制器中你可以保存 $this->request->data['report']
编辑
此外,如果第一个选项不起作用,请尝试使用 FypCakephp 而不是在视图中报告。
它必须是你的模型的别名,在你的例子中是 FypCakephp。
保持 model 和 table 具有各种名称也不是一个好主意,并尝试保持 cakephp 的名称约定(table name report 而不是 Report),但这可能有助于理解所有选项:)
我是 Cakephp 的新手,我实际上遵循了博客教程的教程,但是我没有遵循他们的数据库并尝试自己的。 目前我的数据库:
我的模特:
class FypCakephp extends AppModel {
//Table Name
public $useTable = 'Report';
public $primaryKey = 'report_id';}
我的控制器:
public function add() {
if ($this->request->is('POST')) { //Submited the form:takes a single argument, which can be the request METHOD (get, put, post, delete) or some request identifier (ajax).
$this->FypCakephp->create();// Create a model
// can use the pr() or debug() functions to print it out if you want to see what it looks like.
echo pr(($this->request->data));
debug($_POST);
if ($this->FypCakephp->save($this->request->data)) { //Save to Database and return true.
$this->Session->setFlash(__('Your post has been saved.'));
// return $this->redirect(array('action' => 'index')); //Redirect to the first page
}
$this->Session->setFlash(__('Unable to add your Database.')); //Else will show unable to add
}
}
我的看法:
http://gyazo.com/a69dd4cdfcf008b76d5c3dc5392d2009
每次我调试他们都会给我看这个 http://gyazo.com/7f8f43cbd88aee555aeb6690f33093e0
他们永远接受了 0 个查询。 当我将日期时间更改为文本类型时。他们也只能插入 created 和 modified 。 请问是什么原因造成的?
@初学者。我看到了上面的代码。从cakephp的形式来看,我认为它的结构不太好。初学者阶段请先阅读CakePHP 2.x documentation。然后尝试开发一个基本的 crud 应用程序。 我给你一个postlink。
Creating a CakePHP CRUD Example – Source Code Download and Tutorial
更新 - 1
根据您的代码将 datetime
保存到数据库
public function add() {
if ($this->request->is('POST')) {
//Submited the form:takes a single argument, which can be the request METHOD (get, put, post, delete) or some request identifier (ajax).
$this->FypCakephp->create();// Create a model
$data = $this->request->data;
$updated_date = date('Y-m-d H:m:i', strtotime($data['FypCakephp']['updated_date']));
$data['FypCakephp']['updated_date'] = $updated_date;
// can use the pr() or debug() functions to print it out if you want to see what it looks like.
echo pr($data);
debug($_POST);
if ($this->FypCakephp->save($data)) { //Save to Database and return true.
$this->Session->setFlash(__('Your post has been saved.'));
// return $this->redirect(array('action' => 'index')); //Redirect to the first page
}
$this->Session->setFlash(__('Unable to add your Database.')); //Else will show unable to add
}
}
您的问题正在查看:) 将报告更改为报告。 现在,当您保存 $this->request->data 时,它会尝试查找 Report key 以便能够在您的数据库中保存或报告字段。 如果你不想改变这个,在控制器中你可以保存 $this->request->data['report']
编辑 此外,如果第一个选项不起作用,请尝试使用 FypCakephp 而不是在视图中报告。 它必须是你的模型的别名,在你的例子中是 FypCakephp。 保持 model 和 table 具有各种名称也不是一个好主意,并尝试保持 cakephp 的名称约定(table name report 而不是 Report),但这可能有助于理解所有选项:)