Revision #3 has been created by jonah on Feb 15, 2009, 3:59:16 AM with the memo:
« previous (#1) next (#4) »
Automate timestamps in ActiveRecord models
There are dozens of ways to automate the setting of timestamps in yii ActiveRecord models
, for example with behaviours or onBeforeSave methods. I sort of like the following method.
To start off we need a database table.
magic happens in the rules of the Node class
[...]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.
I find it a
simple and elegant solution
to this issue.