I have a couple of tables Post and Vote. Each post can be voted up or down by users. Below is the table schema.
Post => id, content, user_id
Vote => id, post_id, user_id, type (0->DOWN, 1->UP)
Relation in post model are defined as below.
public function relations()
{
return array(
...
...
'upVoteCount' => array(self::STAT, 'Vote', 'story_id',
'condition'=>'vote='.Vote::VOTE_UP),
'downVoteCount' => array(self::STAT, 'Vote', 'story_id',
'condition'=>'vote='.Vote::VOTE_DOWN),
...
...
);
}
Now, I want to list the post by number of up or down vote counts, e.g. upVoteCount or downVoteCount. I have written the following code in Post controller for displaying posts by highest up vote count but apparantely it’s not working.
public function actionTop($duration = 365, $limit = 100)
{
$dataProvider=new CActiveDataProvider('Post', array(
'criteria'=>array(
'condition'=>'created>=:created',
'params'=>array(
':created'=>strtotime('-'.$duration.' days')
),
'with'=>array('upVoteCount'),
'order'=>'upVoteCount DESC',
'limit'=>$limit
),
'pagination'=>array(
'pageSize'=>20
)
)
);
$this->render('index',array(
'dataProvider'=>$dataProvider,
));
}
May be I need to join vote and post table somehow. Right now, I am getting unknown column upVoteCount in order clause. I am not good at writing SQL which is one of the reason I rely on frameworks to do majority of that work for me