dssqq82402 2017-01-20 13:02
浏览 538

Yii2查询OR条件为1列的多个值

I've made an API with the Yii2 framework. But I don't know how to use the OR condition in my statement.

For example: I want to get all cars with brand BMW or DODGE.

I've tried the following:

$query = Car::getCar($lang)
         ->where(['or', ['m.brand' => 'BMW'], ['m.brand' => 'DODGE']])
         ->all();

But this doesn't work. I only get it to work with one value for m.brand.

So:

$query = Car::getCar($lang)
         ->where(['m.brand' => 'BMW'])
         ->all();

Works just fine.

Tried to put in a few other ways, but I don't get this to work.
Does anyone know what I'm doing wrong?

EDIT The getCar method returns something like:

(new Query())->select(['a.auto_id'])->from('auto_new a') 

EDIT 2 Got it to work with:

$query->andWhere(['or', ['m.brand' => 'BMW'], ['m.brand' => 'DODGE']])
  • 写回答

3条回答 默认 最新

  • dsubq24666 2017-01-20 13:53
    关注

    If I understand you well, you could use something like this:

    Model::find()->orWhere(['brand' => 'brand1])
                 ->orWhere(['id' => 'brand2'])->all();
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题