S
S
Seva2017-08-05 19:51:37
MySQL
Seva, 2017-08-05 19:51:37

CDbCriteria and sorting?

Good day!
There are three tables - articles, translations and comments.
Links:
Article Model

public function relations() {
        return array(
            'translations' => array(self::HAS_MANY, 'ArticleTranslation', 'articleId'),
            'comments' => array(self::HAS_MANY, 'Comment', 'objectId'),
        );
    }

Model Comments
public function relations() {
        return array(
            'article' => array(self::BELONGS_TO, 'Article', 'objectId'),
        );
    }

translations model
public function relations() {
        return array(
            'article' => array(self::BELONGS_TO, 'Article', 'articleId'),
        );
    }

You need to make a selection across all three tables, and sort the articles in descending order of the number of likes (by the likes field from the comments table ) of their comments.
I try:
$criteria = new CDbCriteria();
        $criteria->addCondition('t.Category > 0');
        $criteria->limit = 3;
        $criteria->with = array(
          'comments' => array(
            'select' => array(
              'commentText',
              'likes'
            )
          ),
          'translations' => array(
            'select' => 'title'
          )
        );
        $articleTranslations = Article::model()->findAll($criteria);
        $data['comments'] = $articleTranslations;

As soon as I try something like - falls with an error. I suffer for the third hour, how to win? Thank you! $criteria->order = 'comments.likes DESC'

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question