今天調試YII項目的時候,遇到一個奇葩的事兒,在調試 where or 查詢的時候:調試語句是這樣:
$str = static::find()->where(['or','username' => $username,'mobile' => $account]); echo "<br>"; echo $str->createCommand()->getRawSql();
很明顯,我要到這個數據庫里面找到 username等於$username,或者是mobile等於$account的數據。但是這樣得到的SQL是:
SELECT * FROM `user` WHERE (dd_18314416390) OR (18314416390)
很明顯是不對的,即使是放到MYSQL上執行,也是錯的。
究其原因:yii where or 寫錯啦!
應該這樣:
$str = static::find()->where(['or',['username'=>$username],['mobile' => $account]]); echo "<br>"; echo $str->createCommand()->getRawSql();
這樣生成的SQL是這樣的:
SELECT * FROM `user` WHERE (`username`='dd_18314416390') OR (`mobile`='18314416390')
這樣就對啦!