如何删除与其他有外键关系的All()记录table
How to deleteAll() records which has a foreign key relationship with other table
我需要删除 agency_permissions
中的所有记录 permissions
table 中的 module_type 1
。如何在Yii2中实现,有点类似于deleteAll()->joinWith()
而不是直接使用sql删除命令。寻找实现此任务的 Yii2 方法。下面是 tables:
权限
+-----+-----------------------------------------------+-------------+
| id | title | module_type |
+-----+-----------------------------------------------+-------------+
| 134 | Case / Container | 1 |
| 141 | Container > Status | 1 |
| 146 | Container > Topic/Sub-topic | 1 |
| 150 | Container > Facility/ Sub-facility | 1 |
| 275 | Allow other cities to compare with this city? | 0 |
| 276 | Activate Outlook Module (choose yes) | 0 |
+-----+-----------------------------------------------+-------------+
agency_permissions
+----+---------+---------------+
| id | govt_id | permission_id |
+----+---------+---------------+
| 1 | 22 | 134 |
| 2 | 22 | 141 |
| 3 | 22 | 146 |
| 4 | 22 | 150 |
| 5 | 22 | 275 |
| 6 | 22 | 276 |
+----+---------+---------------+
如果您同时拥有 Permissions.php
和 AgencyPermissions.php
型号或类似型号,您可以这样做:
$permissions = Permissions::find()
->select('id')
->asArray()
->where(['module_type' => 1])
->all();
$permissionsIds = ArrayHelper::getColumn($permissions, 'id');
$rowsDeleted = AgencyPermissions::deleteAll(['permission_id' => $permissionsIds]);
我需要删除 agency_permissions
中的所有记录 permissions
table 中的 module_type 1
。如何在Yii2中实现,有点类似于deleteAll()->joinWith()
而不是直接使用sql删除命令。寻找实现此任务的 Yii2 方法。下面是 tables:
权限
+-----+-----------------------------------------------+-------------+
| id | title | module_type |
+-----+-----------------------------------------------+-------------+
| 134 | Case / Container | 1 |
| 141 | Container > Status | 1 |
| 146 | Container > Topic/Sub-topic | 1 |
| 150 | Container > Facility/ Sub-facility | 1 |
| 275 | Allow other cities to compare with this city? | 0 |
| 276 | Activate Outlook Module (choose yes) | 0 |
+-----+-----------------------------------------------+-------------+
agency_permissions
+----+---------+---------------+
| id | govt_id | permission_id |
+----+---------+---------------+
| 1 | 22 | 134 |
| 2 | 22 | 141 |
| 3 | 22 | 146 |
| 4 | 22 | 150 |
| 5 | 22 | 275 |
| 6 | 22 | 276 |
+----+---------+---------------+
如果您同时拥有 Permissions.php
和 AgencyPermissions.php
型号或类似型号,您可以这样做:
$permissions = Permissions::find()
->select('id')
->asArray()
->where(['module_type' => 1])
->all();
$permissionsIds = ArrayHelper::getColumn($permissions, 'id');
$rowsDeleted = AgencyPermissions::deleteAll(['permission_id' => $permissionsIds]);