Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2 : how to use deleteAll with two conditions = and NOT IN

I am trying to delete data from RestoFoods model like that:

RestoFoods::deleteAll(["restaurant_id"=>$postData['resto_id'], 'food_id NOT IN'=> [1,2] ]);

I want this sql:

DELETE FROM `resto_foods` WHERE `restaurant_id`=1 AND (`food_id` NOT IN (1, 2));
like image 862
Vaseem Khan Avatar asked Nov 26 '15 09:11

Vaseem Khan


1 Answers

You can try this way

RestoFoods::deleteAll(['AND', 'restaurant_id = :restaurant_id', ['NOT IN', 'food_id', [1,2]]], [':restaurant_id' => $postData['resto_id']]);

Output for this will be you want:

DELETE FROM `resto_foods` WHERE (restaurant_id = 1) AND (`food_id` NOT IN (1, 2));
like image 175
GAMITG Avatar answered Oct 16 '22 02:10

GAMITG