Automating timestamps
Almost every model representing content should have creation and modification dates to show the content actuality, revisions, and so on. In Yii there are two good ways to automate this, which are as follows:
Overriding
beforeValidateUsing the
CTimestampBehaviorbehavior from Zii
We will see how to apply these to blog posts. We will use Unix timestamps to store the date and time.
Getting ready
Create a new application by using
yiic webappas described in the official guide at the following URL:http://www.yiiframework.com/doc/guide/en/quickstart.first-app
Set up a database connection and create a table named
postas follows:DROP TABLE IF EXISTS `post`; CREATE TABLE IF NOT EXISTS `post` ( `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, `title` VARCHAR(255) NOT NULL, `text` TEXT NOT NULL, `created_on` INT(10) UNSIGNED NOT NULL, `modified_on` INT(10) UNSIGNED NOT NULL, PRIMARY KEY (`id`) );
Generate a
Postmodel using Gii.Remove everything about
created_onandmodified_on...