Skip to main content

Posts

Showing posts from December, 2011

yii, How to automate timestamps in ActiveRecord models

There are many ways to automate the setting of timestamps in yii ActiveRecord models. Two of them are: Via rules() Via beforeSave() To start off we need to create a database table. CREATE TABLE IF NOT EXISTS ` Nodes ` ( ` id ` bigint ( 20 ) NOT NULL auto_increment , ` title ` varchar ( 255 ) NOT NULL , ` created ` datetime NOT NULL , ` modified ` datetime NOT NULL , PRIMARY KEY ( ` id ` ) ) ; After we have the table we need to create a model and CRUD for it by using Gii. Check the guide on  How to generate model and CRUD with Gii . The first way you can do it is via your model's rules. Here is an example. /* * * @return array validation rules for model attributes. */ public function rules ( ) { return array ( array ( ' title ' , ' length ' , ' max ' => 255 ) , array ( ' title, created, modified ' , ' required ' ) , array ( ' modified '