This post covers my notes on Adding User Comments (chapter 8) in Yii from the book "Web Application Development with Yii and PHP" by Jeffrey Winesett about learning Yii by taking a step-by-step approach to building a Web-based project task tracking system from conception through production deployment - software development life cycle (SDLC) issue-management application.

  • Comments design & develop
  • Creating the widget RecentComments

The following is a list of high-level tasks need to complete:

  • Design and create a new database table to support comments, Yii AR class.
  • Add a form directly to the issue details page to allow users to submit comments.
  • Display a list of all comments associated with an issue directly on its details page.
  • Take advantage of Yii widgets to display a list of the most recent comments on the projects listing page.
class m120921_015630_c reate_u ser_c omments_table extends CDbMigration
    public function up()
        //create the issue table
        $this->createTable('tbl_comment', array(
            'id' => 'pk',
            'content' => 'text NOT NULL',
            'issue_id' => 'int(11) NOT NULL',
            'create_time' => 'datetime DEFAULT NULL',
            'create_user_id' => 'int(11) DEFAULT NULL',
            'update_time' => 'datetime DEFAULT NULL',
            'update_user_id' => 'int(11) DEFAULT NULL',
         ), 'ENGINE=InnoDB');
        //the tbl_comment.issue_id is a reference to 
        $this->addForeignKey("fk_comment_issue", "tbl_comment", "issue_id", "tbl_issue", "id", "CASCADE", "RESTRICT");
        //the tbl_issue.create_user_id is a reference to 
        $this->addForeignKey("fk_comment_owner", "tbl_comment", "create_user_id", "tbl_user", "id", "RESTRICT", "RESTRICT");
        //the tbl_issue.updated_user_id is a reference to 
        $this->addForeignKey("fk_comment_update_user", "tbl_comment", "update_user_id", "tbl_user", "id", "RESTRICT", "RESTRICT");
    public function down()
        $this->dropForeignKey('fk_comment_issue', 'tbl_comment');
        $this->dropForeignKey('fk_comment_owner', 'tbl_comment');
        $this->dropForeignKey('fk_comment_update_user', 'tbl_comment');
//relations OK, add to Issue comment relation
public function relations()
return array(
'requester' => array(self::BELONGS_TO, 'User', 'requester_id'),
'owner' => array(self::BELONGS_TO, 'User', 'owner_id'),
'project' => array(self::BELONGS_TO, 'Project', 'project_id'),
'comments' => array(self::HAS_MANY, 'Comment', 'issue_id'),
'commentCount' => array(self::STAT, 'Comment', 'issue_id'),
/*This establishes the one-to-many relationship between an issue and comments. It
also defines a statistical query to allow us to easily retrieve the total comment count
for any given issue instance*/
//add to IssueController:
    protected function createComment($issue)
        $comment=new Comment;  
                Yii::app()->user->setFlash('commentSubmitted',"Your comment has been added." );
        return $comment;

Creating the widget RecentComments

 * RecentCommentsWidget is a Yii widget used to display a list of recent comments 
class RecentCommentsWidget extends CWidget
    private $_comments;  
    public $displayLimit = 5;
    public $projectId = null;
    public function init()
        if(null !== $this->projectId)
            $this->_comments = Comment::model()->with(array('issue'=>array('condition'=>'project_id='.$this->projectId)))->recent($this->displayLimit)->findAll();
            $this->_comments = Comment::model()->recent($this->displayLimit)->findAll();
    public function getData()
        return $this->_comments;
    public function run()
        // this method is called by CController::endWidget()    
    <?php foreach($this->getData() as $comment): ?>  
        <div class="author">
            <?php echo $comment->author->username; ?> added a comment.
        <div class="issue">      
           <?php echo CHtml::link(CHtml::encode($comment->issue->name), array('issue/view', 'id'=>$comment->issue->id)); ?>
    <?php endforeach; ?>

Download source code of chapter 8

Leave a Comment

Fields with * are required.

Please enter the letters as they are shown in the image above.
Letters are not case-sensitive.