Yii2 How to perform where AND or OR condition grou

2019-03-09 07:45发布

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,5,8) AND (u.status = 1 OR u.verified = 1) OR (u.social_account = 1 AND u.enable_social = 1)

Thanks

7条回答
聊天终结者
2楼-- · 2019-03-09 08:46

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

查看更多
登录 后发表回答