Yii2错误的活动记录调试工具栏中的查询语法

时间:2017-04-07 22:15:31

标签: mysql yii2 yii2-advanced-app

我对活动记录进行了查询,但对于我的数据查询不起作用,我的网格视图为空。我尝试手动完成,结果还可以。我找到并找到了问题的原因,比较一下错误的sql查询,请参阅下面的代码。

有效记录查询:

`ask_policy`.`id`='ask_po.id_policy'

上面的代码不起作用。问题是

SELECT `ask_policy`.`policy_num`, `ask_po`.po_num, ask_client.insuranced_name FROM `ask_policy`  INNER JOIN `ask_po` ON `ask_policy`.`id` = `ask_po`.`id_policy`  INNER JOIN `ask_client`  ON `ask_policy`.`id_client` = `ask_client`.`id`  WHERE (`ask_policy`.`id`=`ask_po`.`id_policy`) AND (`id_client`=930);

**应该是:**

$query->innerjoinWith(['client'])->where(['id_client'=>Yii::$app->user->identity->id_client])
           ->innerJoinWith(['askPos'])
            ->andWhere(['ask_policy.id'=>'id_policy'])->all();

或如下所示的有效记录查询:

SELECT `ask_policy`.* FROM `ask_policy` INNER JOIN `ask_client` ON `ask_policy`.`id_client` = `ask_client`.`id` INNER JOIN `ask_po` ON `ask_policy`.`id` = `ask_po`.`id_policy` WHERE (`id_client`=930) AND (`ask_policy`.`id`='ask_po.id_policy')

在yii debug中显示我的sql:

SELECT `ask_policy`.`policy_num`, `ask_po`.po_num, ask_client.insuranced_name FROM `ask_policy` 
INNER JOIN `ask_client` ON `ask_policy`.`id_client` = `ask_client`.`id` 
INNER JOIN `ask_po` ON `ask_policy`.`id` = `ask_po`.`id_policy` 
WHERE (`id_client`=930) AND (**`ask_policy`.`id`=`id_policy`**)

**应该是:**

Microsoft.WindowsAzure.Storage

为什么上面的代码可以与手动sql查询不同?我在使用它时犯了错误吗?

1 个答案:

答案 0 :(得分:0)

在andWhere条件下更新您的查询。不要在这里传递数组而不是传递字符串,如下所示。

 $query-> innerJoinWith(['askPos'])
      ->andwhere("ask_policy.id = ask_po.id_policy")
      ->innerjoinWith(['client'])->andWhere(['id_client'=>Yii::$app->user->identity->id_client])->all();