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 想编写一个期货跨期套利的程序
  • ¥15 UltraScale 系列 Bitslip 技术支持
  • ¥15 一个线程在sleep的时候set一个信号会起作用吗
  • ¥100 需求高精度PT100设计电路和算法
  • ¥15 单片机配网,继电器开关,广播
  • ¥60 Qcustomplot绘制实时动态曲线
  • ¥20 运用matlab画x-y图
  • ¥15 用idea运行项目,运行tomcat报错:断言失败
  • ¥15 Sqlserver查询链接服务器数据问题
  • ¥15 Bibtex4Word 引用中文文献