Tuesday, February 8, 2022

[FIXED] How to delete an ActiveRecord in Yii2

Issue

I have a quick question. I have a model that I gotta get rid off. I have tried something like the following, but I get an error called Call to a member function delete() on array:

This is what I have tried so far.

$foundTeams = UserHasTeam::find()->where(['user_has_team.user_iduser' => $model->id])->all();
$foundTeams->delete();

Solution

There is static method deleteAll() for this in ActiveRecord.

UserHasTeam::deleteAll(['user_iduser' => $model->id]);

This will delete all rows from UserHasTeam::tableName() where user_iduser is $model->id.



Answered By - Serghei Leonenco

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.