在 CakePHP 中提交当前时间戳

2021-12-21 00:00:00 php cakephp cakephp-1.3

直接在 INSERTUPDATE 上提交当前时间戳的方法是什么?如果我运行的是常规 SQL,我会在提交时使用函数 NOW() 来处理特定的 SQL 字段.我将如何使用 CakePHP 做到这一点?

What is the method to submit a current timestamp directly on an INSERT or an UPDATE? If I were running regular SQL, I would use the function NOW() for the specific SQL field on submission. How would I do that with CakePHP?

$this->Model->save($this->data)

推荐答案

在 CakePHP 中,您可以包含使用 DboSource::expression 未转义的 NOW() 函数

In CakePHP, you can include the NOW() function unescaped by using DboSource::expression

$this->data['SomeModel']['your_datetime_field'] = DboSource::expression('NOW()');
$this->Model->save($this->data);

这是在您的保存中包含 MySQL 函数的首选方式.

This is the preferred way of including MySQL functions in your saves.

http://api.cakephp.org/2.3/class-DboSource.html#_表达式

相关文章