How should I set Creation/Update timestamps

In the documentaion three method for setting timestamps is proposed: behaviours, events and delegating to RDBS (skiping on model).

When we should each of them? May be use together?



34.0k

I suggest you to use behaviors and relay on the application timezone settings, not on database.

    public function initialize()
    {
        $this->addBehavior(new Timestampable(array(
            'beforeValidationOnCreate' => array(
                'field' => 'created_at',
                'format' => 'Y-m-d H:i:s',
            ),
            'beforeValidationOnUpdate' => array(
                'field' => 'updated_at',
                'format' => 'Y-m-d H:i:s',
            ),
        )));
    }