PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, February 8, 2022

[FIXED] How to delete an ActiveRecord in Yii2

 February 08, 2022     php, yii, yii2     No comments   

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
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

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

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing