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?
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.
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.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With