我正在使用Cakephp 3.0.2。我想补充一条新纪录。完整记录error_reporting(-1);
ini_set('display_errors', 'On');
看起来像这样;
$this->request->data
但是,我希望HTTP发布'date_taken' => [
'month' => '01',
'day' => '05',
'year' => '2015',
'hour' => '02',
'minute' => '51'
],
'moves_per_minute' => '80',
'person_id' => '1'
,使其看起来像这样;
$this->request->data
'moves_per_minute' => '80',
'person_id' => '1'
数据应使用当前时间自动更新。
我将列dates_taken
定义为DATETIME类型,默认为CURRENT_TIME_STAMP。当我执行HTTP发布并发送dates_taken
而不是$this->request->data
为空时,无法添加新行。
这就是我的控制器代码的样子;
date_taken
正确的控制器应该如何?
答案 0 :(得分:1)
将“created”和“modified”列添加到数据库表(均为DATETIME),然后在表的模型中添加以下行:
class ArticlesTable extends Table
{
public function initialize(array $config)
{
$this->addBehavior('Timestamp');
// Timestamp is the correct, even though the field is a datetime
}
}
添加新记录后,存储在创建字段中的日期时间将是创建的确切时间,修改后的字段将是您最后一次修改记录。每次通过实体访问此记录时,Cake都会默认执行此操作。
可以通过此处的时间戳文档找到更多信息:http://book.cakephp.org/3.0/en/orm/behaviors/timestamp.html
您还可以使用时间库保存当前时间,并在保存之前在实体中手动设置:
$time = Time::now();
$newRecord->date_taken = $time;
$this->newRecords->save($newRecord);
再次假设date_taken字段是表中的日期时间对象。不过,我建议让数据库为你设置它而不是这个方法。