Ordering by relational STAT in yii

I am using yii framework. I have an attitude

'revCount'=>array(self::STAT, 'Review','rid','condition'=>'status=1')//count review for each restaurant

      

Now I want to get a list of all restaurants by number of views.

Restaurant::model()->findAll('with'=>'revCount');

      

What else do I need to add to get all restaurants ordered by reviews?

+3


source to share


2 answers


This is how you join other models with one request:

Restaurant::model()->with('revCount')->findAll();

      



But I think you want to not join the model, but just sort it. Similar to this:

Restaurant::model()->with(array(
    'reviews'=>array(
        // we don't want to select reviews
        'select'=>false,
        // but want to get only reviews with status=1
        'joinType'=>'INNER JOIN',
        'condition'=>'reviews.status=1',
    ),
))->findAll();

      

+1


source


You should be able to do the following.



Restaurant::model()->findAll('with'=>'revCount', 'select' => '(SELECT COUNT(*) FROM review WHERE rid = t.id AND status = 1) AS revCount');

      

+1


source







All Articles