如何在Yii ActiveRecord中编写OR表达式?

时间:2015-07-30 19:58:09

标签: php activerecord yii2

我尝试了以下操作,但它没有用。

  

$ kw = \ app \ models \ Keyword :: find() - > where(['keyword'=> $ word,['或',['country'=> $ country],[' country'=> null]],['或',['language'=> $ language],['language'=> null]]]);

我也试过

  

$ kw = \ app \ models \ Keyword :: find() - > where(['keyword'=> $ word,['或','country'=> $ country,'country'= > null],['或','language'=> $ language,'language'=> null]]);

不负责任的错误:

  

PHP警告'yii \ base \ ErrorException',消息'strtoupper()要求参数1为字符串,给定数组'

文档:http://www.yiiframework.com/doc-2.0/yii-db-queryinterface.html#where()-detail

3 个答案:

答案 0 :(得分:0)

我用andWhere()

解决了这个问题
  

$ kw = \ app \ models \ Keyword :: find() - > where([' keyword' => $ word]) - > andWhere(['或&# 39;,[' country' => $ country],[' country' => null]]) - > andWhere(['或', ['语言' => $语言],['语言' => null]]);

产生

  

SELECT * FROM`keyword` WHERE((`keyword` =' women \' s);和(`country` =' US')或(`country` IS NULL)))AND((`language` =' en')OR(`language` IS NULL))ORDER BY`country`,`language`

答案 1 :(得分:0)

您可以使用 orWhere() 来执行此操作:

$kw = \app\models\Keyword::find()->where(['keyword'=>$word])->orWhere(['country' => $country])->orWhere(['language' => $language]);

有关详细信息,请参阅此处

www.yiiframework.com/doc-2.0/yii-db-querytrait.html#orWhere() - detaila

答案 2 :(得分:-1)

尝试使用Keyword::find()代替\app\models\Keyword::find()
我有类似的问题。它对我来说很好。希望它也适合你。