Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I delete rows in Yii2?

Tags:

yii2

I want to delete a row in database based on some condition. I tried like this

$fanclub        =   FanClub::find()->where(['user_id'=>$userid])->
            andwhere(['crew_member_id'=>$id])->one();
            if($fanclub)
            {
                $fanclub->delete();
            }

is this the right way to delete a row in database?

like image 804
Bloodhound Avatar asked Jul 28 '15 09:07

Bloodhound


People also ask

How to delete data in Yii2?

Deleting Data To delete a single row of data, first retrieve the Active Record instance corresponding to that row and then call the yii\db\ActiveRecord::delete() method. $customer = Customer::findOne(123); $customer->delete(); You can call yii\db\ActiveRecord::deleteAll() to delete multiple or all rows of data.

Is null in Yii2 query?

Yii2 will use "IS NULL" if the $values === null , but in case the value is supplied as an array, and one of those array elements is null, it will not get any special treatment, resulting in the query never matching any records with NULL value.


1 Answers

There you are getting record first, but you can do it directly

$fanclub = FanClub::find()
  ->where(['user_id'=>$userid])
  ->andwhere(['crew_member_id'=>$id])
  ->one()
  ->delete();
like image 167
Ramssés Gómez Avatar answered Sep 18 '22 00:09

Ramssés Gómez