简体   繁体   中英

writing query in yii2 with multiple where conditions

query in plain php is

$p = "SELECT * FROM followusers WHERE (follower='$followed1 and
    followed='$follower1') or (follower='$follower1' and 
    followed='$followed1')"

i have written query in yii2 is

 $y = Followusers::find()->where(['follower' => $userid ] and
 ['followed' => $conid])->orwhere(['followed' => $userid ] and
 ['follower' => $conid])-> all();

but im not getting the required result for the query in yii2

Try this way:

Followusers::find()->where(['follower' => $userid])    
   ->andWhere(['followed' => $conid])
   ->orwhere(['AND',
        ['followed' => $userid], 
        ['follower' => $conid]
    ])
   -> all();

It seems like you have some syntax errors in your code, try formatting it as follows:

$y = Followusers::find()
     ->where(['follower' => $userid, 'followed' => $conid])
     ->orwhere(['followed' => $userid, 'follower' => $conid])
     ->all();

If followusers table model is FollowUsers (in the current namespace, otherwise you have to prefix with namespace), you can

$data = FollowUsers::find()
->where(['AND', 'follower='.$followed1, 'followed='.$follower1])
->orWhere(['AND', 'follower='.$follower1, 'followed='.$followed1])
->all();

Otherwise, you can usign parameters:

$data = FollowUsers::find()
->where('(follower=:followed1 AND followed=:follower1) OR (follower=:follower1 AND followed=:followed1)', [':followed1' => $followed1, ':follower1' => $follower1])
->all();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM