2019-08-20 17:06:02 1643次浏览 3条回答 0 悬赏 10 金钱
$query->andFilterWhere([
	'OR',
	['id' => $this->location_id],
	[
		'OR',
		['area_id' => $this->location_id],
		[
			'OR',
			['city_id' => $this->location_id],
			['province_id' => $this->location_id],
		],
	],
]);

最佳答案

  • Cathay 发布于 2019-08-20 17:15 举报

    我觉得可以这样写:

    $query->andFilterWhere([
        'OR', 
        ['id' => $this->location_id], 
        ['area_id' => $this->location_id], 
        ['city_id' => $this->location_id], 
        ['province_id' => $this->location_id]
    ]);
    
  • 回答于 2019-08-20 18:53 举报

    可以,这个参数原来可以写多个啊,我才知道。之前一直以为只能写三个呢。不错,意外收获

  • 回答于 2019-08-20 23:34 举报

    还有一种写法:

    $query->andWhere(new OrCondition([
        new InCondition('type', 'in', $types),
        ['like', 'name', '%good%'],
        'disabled=false'
    ]));
    

    点击:配合数据库工作,查找关键字:OrCondition

    看了下源码,['OR', 操作数1, 操作数2, ...] 最后转换就是用到就是用到类OrCondition

    相关文件及关键代码:
    文件:Class yii\db\QueryBuilder

    代码:

        /**
         * Contains array of default condition classes. Extend this method, if you want to change
         * default condition classes for the query builder. See [[conditionClasses]] docs for details.
         *
         * @return array
         * @see conditionClasses
         * @since 2.0.14
         */
        protected function defaultConditionClasses()
        {
            return [
                'NOT' => 'yii\db\conditions\NotCondition',
                'AND' => 'yii\db\conditions\AndCondition',
                'OR' => 'yii\db\conditions\OrCondition',
                'BETWEEN' => 'yii\db\conditions\BetweenCondition',
                'NOT BETWEEN' => 'yii\db\conditions\BetweenCondition',
                'IN' => 'yii\db\conditions\InCondition',
                'NOT IN' => 'yii\db\conditions\InCondition',
                'LIKE' => 'yii\db\conditions\LikeCondition',
                'NOT LIKE' => 'yii\db\conditions\LikeCondition',
                'OR LIKE' => 'yii\db\conditions\LikeCondition',
                'OR NOT LIKE' => 'yii\db\conditions\LikeCondition',
                'EXISTS' => 'yii\db\conditions\ExistsCondition',
                'NOT EXISTS' => 'yii\db\conditions\ExistsCondition',
            ];
        }
    
    1 条回复
    回复于 2019-08-21 08:42 回复

    够深入,赞一个!

您需要登录后才可以回答。登录 | 立即注册
阿刚
见习主管

阿刚 深圳

注册时间:2016-04-04
最后登录:2024-02-22
在线时长:15小时6分
  • 粉丝4
  • 金钱185
  • 威望0
  • 积分335

热门问题