How to automate timestamps in ActiveRecord models

Comparing #5 with #6

Revision #6 was created by wei wei on Feb 17, 2009, 11:32:10 AM.

add new line to remove horizontal scrolling code blocks

Content

[...]
public function rules()
{
return array(
array('title','length','max'=>255),
array('title, created, modified', 'required'),
array('modified','default',

 
                  
'value'=>new CDbExpression('NOW()'),
 
                  
'setOnEmpty'=>false,'on'=>'update'), array('created,modified','default',
 
                  
'value'=>new CDbExpression('NOW()'),
 
                  
'setOnEmpty'=>false,'on'=>'insert')
);
}

You see the two rules at the end, one changes the modified field when the record's being updated, and the other changes both fields when the record's being created. You'll also see the "new CDbExpression('NOW()')" statement. This passes "NOW()" to the MySQL server and it will not be escaped. MySQL will interpret it as a statement and not as a string. This means that the field types could have been any other date/time type (timestamp, etc.) and it would still work.
[...]