dongya2029 2018-10-11 11:22
浏览 43
已采纳

Yii第二个查询参数

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)
  • 写回答

1条回答 默认 最新

  • duanlanzhi5509 2018-10-11 11:30
    关注

    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 the post.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'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?