searching for self::STAT relation

Hi all,

The relation between ‘Post’ model and ‘Comment’ model is one to many, and the relations of ‘Post’ is defined as:




public function relations()

{

   return array(

      'comments' => array(self::HAS_MANY, 'Comment', 'postID'),

      'commentCount' => array(self::STAT, 'Comment', 'Comment(postID, commentID)'),

   );

}



Now, the commentCount is display in a ‘Post’ CGridView. How to write the CDbCriteria so that

the field can be searched and sorted?

I tried the following but Yii complains ‘Unknown column in SQL …’:




public function search()

{

   $criteria = new CDbCriteria;

   ...

   $criteria->with = array('commentCount');

   $criteria->compare('commentCount', $this->commentCount);


   return new CActiveDataProvider($this, array(

      'criteria' => $criteria,

   ));

}



Thanks.




  public function relations() {

        return array(

            'comments' => array(self::HAS_MANY, 'Comment', 'postId'),

            'numComments' => array(self::STAT, 'Comment', 'postId'),

        );

    }



Emily is right in the declaration of the relation.

But you can not use STAT relation in searching or sorting.

We have to construct the ‘select’, ‘where’ and ‘order by’ clauses for it by hand.

http://www.yiiframework.com/forum/index.php/topic/8015-order-by-count-using-stat-relation