I have query to my database using Yii
$query = Task::find()
->where([Task::tableName().'.parent_id' => 0]);
$query->leftJoin(TaskModel::tableName(), [TaskModel::tableName().'.task_id' => Task::tableName().'.id']);
$query->andWhere([TaskModel::tableName().'.format' => null]);
$rows = $query
->offset($pages->offset)
->limit($pages->limit)
->orderBy([
Task::tableName().'.status' => SORT_DESC,
Task::tableName().'.`estimated_date`=0' => SORT_ASC,
Task::tableName().'.estimated_date' => SORT_ASC
])
->all();
But that doesn't work, because at leftJoin, when I set relations between tables, second parameter sets string.
rawSql of my query:
SELECT `task`.*
FROM `task` LEFT JOIN `task_model`
ON `task_model`.`task_id`='{{%task}}.id'
WHERE (`task`.`parent_id`=0) AND (`task_model`.`task_id` IS NULL)
You should use string in this case:
$query->leftJoin(TaskModel::tableName(), TaskModel::tableName() . '.task_id = ' . Task::tableName() . '.id');
This case is explicitly mentioned in documentation:
Note that the array format of
where()
is designed to match columns to values instead of columns to columns, so the following would not work as expected:['post.author_id' => 'user.id']
, it would match thepost.author_id
column value against the string'user.id'
. It is recommended to use the string syntax here which is more suited for a join:'post.author_id = user.id'