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
beforeValidate
Using the
CTimestampBehavior
behavior from Zii
We will see how to apply these to blog posts. We will use Unix timestamps to store the date and time.
Create a new application by using
yiic webapp
as 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
post
as 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
Post
model using Gii.Remove everything about
created_on
andmodified_on...