Also available in these languages:
English日本語polskiРусский简体中文

Customizing Comment Model

For the Comment model, we mainly need to customize the rules() and attributeLabels() methods. The attributeLabels() method returns a mapping between attribute names and attribute labels. We do not need to touch relations() since the code generated by the yiic tool is good enough.

Customizing rules() Method

We first customize the validation rules generated by the yiic tool. The following rules are used for comments:

public function rules()
{
    return array(
        array('content, author, email', 'required'),
        array('author, email, url', 'length', 'max'=>128),
        array('email','email'),
        array('url','url'),
    );
}

In the above, we specify that the author, email and content attributes are required; the length of author, email and url cannot exceed 128; the email attribute must be a valid email address; and the url attribute must be a valid URL.

Customizing attributeLabels() Method

We then customize the attributeLabels() method to declare the label display for each model attribute. This method returns an array consisting of name-label pairs. When we call CHtml::activeLabel() to display an attribute label.

public function attributeLabels()
{
    return array(
        'id' => 'Id',
        'content' => 'Comment',
        'status' => 'Status',
        'create_time' => 'Create Time',
        'author' => 'Name',
        'email' => 'Email',
        'url' => 'Website',
        'post_id' => 'Post',
    );
}

Tip: If the label for an attribute is not declared in attributeLabels(), an algorithm will be used to generate an appropriate label. For example, a label Create Time will be generated for attributes create_time or createTime.

Customizing Saving Process

Because we want to record the creation time of a comment, we override the beforeSave() method of Comment like we do for the Post model:

protected function beforeSave()
{
    if(parent::beforeSave())
    {
        if($this->isNewRecord)
            $this->create_time=time();
        return true;
    }
    else
        return false;
}
$Id: comment.model.txt 1733 2010-01-21 16:54:29Z qiang.xue $
If you find any typos or errors in the tutorial, please create a Yii ticket to report it. If it is a translation error, please create a Yiidoc ticket, instead. Thank you.

Total 2 comments:

#79
Markdown generated in beforeValidate instead of beforeSave
by jonah at 2:53pm on February 16, 2009.

The reason that post content is parsed with CMarkdownParser in beforeValidate() instead of beforeSave() (as you might think), is so that the content will be parsed when using the "preview" button on the post creation page.

#355
Question
by Ismael at 6:24pm on June 3, 2009.

It's written here: http://www.yiiframework.com/doc/api/CActiveRecord#afterSave-detail

Make sure you call the parent implementation so that the event is raised properly.

Shouldn't it be done in this example?

Something like: parent::afterSave();

I don't know!

Your Comment:

You may enter comment using Markdown syntax.

Please login with your forum account.
Note: you must have at least ONE forum post with your account.