Yii2 How to perform where AND or OR condition grouping?

后端 未结 8 2315
说谎
说谎 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:43

    I assume that you have already knew about database configuration in Yii 2.0, which is basically the same as in Yii 1.0 version.

    If you want to use activeQuery, you need to define a ‘USERS’ class first:

    
    

    Then when you use it,you can write it as following:

      
                ->where("user_id IN(1,5,8) AND (status = 1 OR verified = 1) OR (social_account = 1 AND enable_social = 1)")
                ->all();    
    ?>
    

    In my opinion, active query provides you a way to separate sql by sub-blocks. But it does not make any sense to apply it when you have such a complicated 'AND OR' WHERE condition..

提交回复
热议问题