删除 YII2 中的多行 [英] Delete multiple rows in YII2
本文介绍了删除 YII2 中的多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个从数据库中获取的对象数组:
I have an array of objects fetched from database:
$masterListContacts = MasterListContacts::find()
->select('master_list_contacts.*')
->innerJoin('master_contacts', '`master_contacts`.`id` = `master_list_contacts`.`master_contact_id`')
->with('masterContact')
->where(['user_id' => \Yii::$app->user->identity->id, 'slug' => $slug])
->all();
在某些情况下,我需要从该数组中表示的数据库中删除所有行.但是使用 delete() 和 deleteAll() 方法时,我得到了一个错误 Call to a member function ... on array
.有人能告诉我哪种方法是实现这一目标的最佳方法吗?
Under certain circumstances, I need to delete all rows from the database represented in this array. But with both delete() and deleteAll() methods I got an error Call to a member function ... on array
. Could someone tell me please which one is the best way to accomplish this?
更新:这里是我的数据库结构.
UPDATE: Here is my database structure.
推荐答案
找到更好的解决方案:
\Yii::$app
->db
->createCommand()
->delete('master_contacts', ['id' => $deletableMasterContacts])
->execute();
其中 $deletableMasterContacts
是 master_contacts id 的数组,应该删除
Where $deletableMasterContacts
is array of master_contacts ids, which should be deleted
这篇关于删除 YII2 中的多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文