Submitting current timestamp in CakePHP

前端 未结 3 1836
忘了有多久
忘了有多久 2020-12-15 07:30

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

相关标签:
3条回答
  • 2020-12-15 07:44

    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);
    

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

    http://api.cakephp.org/2.3/class-DboSource.html#_expression

    0 讨论(0)
  • 2020-12-15 07:49

    You can set timestamp field to auto initialize and auto update

    timestampfield TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
    

    http://dev.mysql.com/doc/refman/5.0/en/timestamp.html

    0 讨论(0)
  • 2020-12-15 07:52

    if you add the created and modified columns in you table they will be automatically populated with current time stamp. If the case is different - i.e. you want to populate a field which later on you want to modify, probably using the edorian's solution is best.

    0 讨论(0)
提交回复
热议问题