There is a sql query:
SELECT * FROM article a INNER JOIN category_article ca ON a.id=ca.article_id WHERE ca.category_id = 2 AND EXISTS( SELECT 1 FROM category_article WHERE article_id = a.id AND category_id = 9 ) For Active Record rewrote it this way:
$query = Article::find(); $query->innerJoinWith(['categories'], false)->andWhere(['category.id' => 2]); $query->andWhere(['exists', (new \yii\db\Query()) ->from('category_article') ->where(['article_id' => 'article.id']) // Указать поле нужно тут ->andWhere(['category_id' => 9]) ]); $query->all() The problem with ActiveRecord is that in ->where(['article_id' => 'article.id']) value of article.id not perceived as the id field of the article table, but as a text value. How can I specify the field of the article table in the condition?