2014-10-06 67 views
0

我堅持這樣的代碼:Yii的查詢生成器,其中相同的條款

->select ('t1.id_i, t2.status') 
->from ('table1 as t1, table2 as t2')   
->where(array('or', 'id_i'=>$model->id, array('like', 't2.status', '%Beginner%'))) 

這裏有我想要的東西

WHERE t1.id_i=$model->id AND/OR t2.status LIKE "Beginner" //AND/OR are optional combination 

我嘗試沒有結果很多組合。

請幫幫我。 謝謝。

回答

0

我認爲這個問題是CDbCommandwhere()方法不使用鍵值對$conditions陣列,你在這裏做支持:'id_i'=>$model->id_e。該綁定需要通過參數$params發生。除此之外,我還建議使用值綁定而不是數組語法的字符串,因爲它會使代碼更易於閱讀。

這裏的呼叫修改後的代碼,以where()

->where('id_i = :id_e OR t2.status LIKE "%Beginner%"', array(':id_e' => $model->id_e)) 

你可以看到,它更接近最終的SQL,這使得調試更加方便。

如果你決定使用原來的語法,試試這個來代替:

->where(array('or', 'id_i = :id_e', array('like', 't2.status', '%Beginner%')), array(':id_e' = $model->id_e)) 
0

儘量把條款在哪裏()方法,在類CDbCommand,其中()的方法指定WHERE部分的查詢,第一個參數是查詢條件,第二個參數是綁定到查詢的參數(name => value)。

->select ('t1.id_i, t2.status') 
->from ('table1 as t1, table2 as t2')   
->where('t1.id_i=:id_i OR t2.status LIKE "%:status%"',array(':id_i' => $model->id,':status' => 'Beginner')) 

更多CDbCommand細節,其中()方法here

+1

部屋,別忘了說明你已經改變了什麼,以及爲什麼。不要忘了堆棧溢出中有許多新手可以從你的經驗中學到一兩件事,而對你來說顯而易見的東西可能不是他們的。 – 2014-10-09 05:36:29