dprfe04886
2015-07-30 19:58
浏览 109
已采纳

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

I tried the following, but it didn't work.

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

I also tried

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

Worthless error:

PHP Warning 'yii\base\ErrorException' with message 'strtoupper() expects parameter 1 to be string, array given'

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

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dqcuq4138 2015-07-30 20:19
    已采纳

    I solved it with andWhere()

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

    which yields

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

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongxian5735 2015-07-31 04:19

    You can use orWhere() to do it:

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

    See this for more detail

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

    评论
    解决 无用
    打赏 举报
  • duanmi4379 2015-07-31 06:05

    Try using Keyword::find() instead of \app\models\Keyword::find()
    I had the similar problem. It worked fine for me. Hope it works for you too.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题