Yii2 How to perform where AND or OR condition grouping?

后端 未结 8 2291
说谎
说谎 2020-12-08 02:02

I am new to Yii-2 framework. How can i achieve following query in Yii-2 framework using activeQuery and models.

SELECT * FROM users AS u WHERE u.user_id IN(1         


        
相关标签:
8条回答
  • 2020-12-08 02:53

    try this -

    $query = (new \yii\db\Query())
                ->select('*')
                ->from('users u')
                ->where(['and',['u.user_id'=>[1,5,8]],['or','u.status=1','u.verified=1']])
                ->orWhere(['u.social_account'=>1,'u.enable_social'=>1]);
        $command = $query->createCommand();
        print_r ($command->sql);die;
    

    more info

    0 讨论(0)
  • 2020-12-08 02:55

    You can try this:

    //SELECT * FROM users AS u WHERE u.user_id IN(1,5,8) AND (u.status = 1 OR u.verified = 1) OR (u.social_account = 1 AND u.enable_social = 1)
    $model = arname()->find()
           ->andWhere(['user_id'=>[1,5,8]])
           ->andWhere(['or',
               ['status'=>1],
               ['verified'=>1]
           ])
           ->orWhere(['and',
               ['social_account'=>1],
               ['enable_social'=>1]
           ])
           ->all();
    
    0 讨论(0)
提交回复
热议问题