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 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.

Getting ready

  1. 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

  2. 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`)
    );
  3. Generate a Post model using Gii.
  4. Remove everything about created_on and modified_on from the rules method of the model.

How to do it...

  1. We will start with overriding the beforeValidate method. Open protected/models/Post.php and add the following method:
    protected function beforeValidate()
    {
       if($this->getIsNewRecord())
          $this->created_on = time();
    
       $this->modified_on = time();
    
       return parent::beforeValidate();;
    }
  2. Now add the following code to the new controller and run it:
    $post = new Post();
    $post->title = "test title";
    $post->text = "test text";
    $post->save();
    echo date('r', $post->created_on);
  3. You should get a date and time. Since we have simply created a post, it will be the current date and time. Another method is to use CTimestampBehavior. Delete the Post model and generate it one more time by using Gii. Remove everything about created_on and modified_on from the rules method of the model. Add the following method to the model:
    public function behaviors()
    {
       return array(
          'timestamps' => array(
             'class' => 'zii.behaviors.CTimestampBehavior',
             'createAttribute' => 'created_on',
             'updateAttribute' => 'modified_on',
             'setUpdateOnCreate' => true,
          ),
       );
    }

How it works...

The beforeValidate method executes just before the model validation starts. In this method, modified_on is always filled and created_on is filled only if the model is new, which is only when we are creating a post.

When we use the ready behavior from Zii, we specify createAttribute and updateAttribute to match the field names we have chosen. The setUpdateOnCreate property triggers filling modified_on when a record is inserted. The rest is done by the behavior function.

There's more...

In order to learn more about CTimestampBehavior, refer to the following API page:

http://www.yiiframework.com/doc/api/CTimestampBehavior/

See also

  • The Processing model fields with AR event-like methods recipe
  • The Applying markdown and HTML recipe
  • The Highlighting code with Yii recipe
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.25.74