Search code examples
sqlyii2not-existsnotin

Yii2: Use NOT EXISTS or NOT IN


How do I use the conditions "not exist" and "not in" in Yii2? I have a select with the list of users, but want to exclude users who are on the table "user_post"

User

id_user | username

User_post

id_user_post | id_post | id_user

 <?= $form->field($model, 'id_user')->dropDownList(
         ArrayHelper::map(User::find()

                ->all(),'id_user','username'),
        ['prompt' => 'Select User']
    ) ?>

Solution

  • You can do it like below:

    User::find()->where(['not in','user_id',[1,2,3]]);
    

    Which returns Users with ID's not in [1,2,3]