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

如何在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`

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Docker容器里已经安装了ssh,但打包迁移到新机器一直容器一直提示unrecognized service。
  • ¥15 综合布线实例设计,就好看好看不恐怖可好滤镜好聚
  • ¥15 使用moviepy库视频合并时出错
  • ¥30 FLUENT液固传质UDF
  • ¥15 怎么看梯度直方图以,怎么判断梯度消失/爆炸,怎么解决
  • ¥15 aspnetdll文件访问拒绝
  • ¥15 wpf中在模版中寻找元素
  • ¥15 MFC平台生成指定圆
  • ¥15 jmeter出现403
  • ¥500 求华为P30PRO手机硬盘数据恢复