Answer the question
In order to leave comments, you need to log in
How to write a similar sql query in yii?
SELECT distinct
post.id as id,
post.authorID as authorID
FROM post left OUTER JOIN comment ON post.id = comment.postID
WHERE comment.authorID <>1;
$dataProvider = new ActiveDataProvider([
'query' => Post::find()->where(['activateStatus' => 1])->andWhere(['closedDate' => NULL])->orderBy('rating DESC'),
'pagination' => [
'pageSize' => 20,
],
]);
Answer the question
In order to leave comments, you need to log in
Goodnight.
There is a SqlDataProvider .
Here is an article on using join in ActiveRecord
See:
the find() method - called on an AR object returns an AR.
In your code:
$query = (new Query())->select([
'post.id' => 'id',
'post.authorID' => 'authorID',
'post.title' => 'title',
'post.content' => 'content',
])
->distinct()
->from('post')
->leftJoin('comment', '`comment`.`postID` = `post`.`id`')
->where(['comment.authorID' => 1])
->with('comments')
->all();
SELECT DISTINCT
"post"."id" AS "id",
"post"."authorID" AS "authorID",
"post"."title" AS "title",
"post"."content" AS "content"
FROM "post"
LEFT JOIN "comment" ON `comment`.`postID` = `post`.`id`
WHERE "comment"."authorID"<>1
$query = (new Query())
->select([
'id' => 'post.id',
'authorID' => 'post.authorID',
'title' => 'post.title',
'content' => 'post.content',
])->distinct()
->from('post')
->leftJoin('comment', '`comment`.`postID` = `post`.`id`')
->where(['<>','comment.authorID', 1]);
Yii::$app->db->createCommand('SELECT distinct
post.id as id,
post.authorID as authorID
FROM post left OUTER JOIN comment ON post.id = comment.postID
WHERE comment.authorID <> :author_id;', [':author_id' => 1])
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question