我正在尝试用简单的博客学习蛋糕.我用蛋糕烘烤来制作我的控制器.Post模型有一个created_date字段,但我想在服务器端设置它(客户端可以篡改它).我添加了几行到添加控制器,如下所示:
public function add() {
if ($this->request->is('post')) {
$this->Post->create();
date_default_timezone_set("UTC");
$this->request->data['Post']['created_date'] = date("c"); //set create date server side
if ($this->Post->save($this->request->data)) {
$this->Session->setFlash(__('The post has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The post could not be saved. Please, try again.'));
}
}
$authors = $this->Post->Author->find('list');
$tags = $this->Post->Tag->find('list');
$this->set(compact('authors', 'tags'));
}
Run Code Online (Sandbox Code Playgroud)
问题是创建日期根本没有保存,它设置为0000-00-00.我读到的一切都说这应该是好的.即使我查看debugkit,它也会显示post数据在数组中具有正确的created_date.
如果您遵循cakephp的约定,那么框架将自动为您填充此字段.您有两种选择:
重命名您想要的字段'created'并将其设置为数据库中的datetime,如果您不通过发布的数据手动传递值,框架将负责其余的工作
用这个: $this->request->data['Post']['created_date'] = DboSource::expression('NOW()');
| 归档时间: |
|
| 查看次数: |
1171 次 |
| 最近记录: |