2017-04-07 51 views
0

我對活動記錄進行了查詢,並且不適用於我的數據查詢我的網格視圖爲空。我嘗試手動完成,結果是好的。我發現和我發現問題的原因,比較有關錯誤的SQL查詢請參閱下面的代碼。Yii2錯誤的活動記錄調試工具欄中的查詢語法

活動記錄查詢:

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



**in yii debug show me sql : ** 
`SELECT `ask_policy`.* 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);` 

上面的代碼無法正常工作。問題是

`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(); 

告訴我SQL:

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') 

**它應該是:**

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`**) 

爲什麼上面的代碼可以是手動的SQL查詢不同?我是否在使用它時犯了錯誤?

回答

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(); 
+0

非常感謝你 –