Yii2 How to perform where AND or OR condition grouping?

后端 未结 8 2288
说谎
说谎 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:30
    User::find()
        ->select('u_id , u_unique_id, u_first_name, u_last_name, u_username, u_email, u_image,u_phone') 
        ->andWhere("u_status != 2  AND u_id != 1 and u_role in (6,7)")
        ->andWhere(
            ['or',
                ['like', 'u_first_name', $searchVal],
                ['like', 'u_last_name', $searchVal],
                ['like', 'u_username', $searchVal],
                ['like', 'u_email', $searchVal]
    
            ]
            )
        ->all();
    
    0 讨论(0)
  • 2020-12-08 02:35

    where() function of ActiveQuery also accepts string as its first parameter that will be used in WHERE condition of the query. So easiest way would be to put those conditions in where()function.

    Assuming you are using ActiveRecord model, you can do something like following in your model.

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

    Of course you should use prepared statements instead of hardcoding values inside query, but above code is just to give you an idea.

    You can find more detail in documentation Here and Here

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

    Use OR condition at first. For example:

    (new \yii\db\Query())
                ->select(['id', 'client', 'ts', 'action'])
                ->from('log_client as log')
                ->orWhere(['action' => 'lock'])
                ->orWhere(['action' => 'rel'])
                ->andWhere(['in', 'client', $IDs])
                ->orderBy(['ts' => SORT_ASC])
                ->all();
    

    It'll be like a "AND...(..OR..)"

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

    U can also do it this way:

    $data = model::find()
    ->andWhere('plz = :plz',[':plz' => 40])
    ->andWhere('plz = :plz',[':plz' => 40000])
    ->all();
    
    0 讨论(0)
  • 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:

    <?php
        namespace app\models;
        use yii\db\ActiveRecord;
    
        class USERS extends ActiveRecord {
            public static function tableName()
            {
                return 'users';
            }
        }
    ?>
    

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

    <?
        $usr_data = USERS::find()->  
                ->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..

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

    With MongoDB:

                $query->andWhere(['and',
                    ['$eq', 'status', 1],
                    ['$in', 'activity', [1, 2]],
                ]);
                $query->orWhere(['$in', 'yourField', $yourArray]);
    

    Tested. Similar with DBMS.

    0 讨论(0)
提交回复
热议问题