mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-13 20:04:05 +00:00
Compare commits
2 commits
f53fcb279f
...
3974ad046e
| Author | SHA1 | Date | |
|---|---|---|---|
| 3974ad046e | |||
|
|
cfae527c93 |
2 changed files with 20 additions and 10 deletions
|
|
@ -9,30 +9,40 @@ use Stancl\Tenancy\Events\TenantDeleted;
|
|||
use Stancl\Tenancy\Listeners\QueueableListener;
|
||||
|
||||
/**
|
||||
* Cleans up pivot records related to the deleted tenant.
|
||||
*
|
||||
* Clean up pivot records related to the deleted tenant.
|
||||
* The listener only cleans up the pivot tables specified
|
||||
* in the $pivotTables property (see the property for details),
|
||||
* and is intended for use with tables that do not have tenant
|
||||
* foreign key constraints with onDelete('cascade').
|
||||
* and is intended for use with tables that do not have tenant foreign key constraints.
|
||||
*
|
||||
* When using foreign key constraints, you'll still have to use ->onDelete('cascade')
|
||||
* on the constraint (otherwise, deleting a tenant will throw a foreign key constraint violation).
|
||||
* That way, the cleanup will happen on the database level, and this listener will essentially
|
||||
* just perform an extra 'where' query.
|
||||
*/
|
||||
class DeleteAllTenantMappings extends QueueableListener
|
||||
{
|
||||
/**
|
||||
* Pivot tables to clean up after a tenant is deleted, in the
|
||||
* ['table_name' => 'tenant_key_column'] format.
|
||||
* Pivot tables to clean up after a tenant is deleted,
|
||||
* formatted like ['table_name' => 'tenant_key_column'].
|
||||
*
|
||||
* Since we cannot automatically detect which pivot tables
|
||||
* are being used, they have to be specified here manually.
|
||||
* you want to clean up, they have to be specified here.
|
||||
*
|
||||
* The default value follows the polymorphic table used by default.
|
||||
* By default, resource syncing uses the tenant_resources table, and the records are associated
|
||||
* to tenants by the tenant_id column (thus the ['tenant_resources' => 'tenant_id'] default).
|
||||
*
|
||||
* To customize this, set this property, e.g. in TenancyServiceProvider:
|
||||
* DeleteAllTenantMappings::$pivotTables = [
|
||||
* 'tenant_users' => 'tenant_id',
|
||||
* // You can also add more pivot tables here
|
||||
* ];
|
||||
*/
|
||||
public static array $pivotTables = ['tenant_resources' => 'tenant_id'];
|
||||
|
||||
public function handle(TenantDeleted $event): void
|
||||
{
|
||||
foreach (static::$pivotTables as $table => $tenantKeyColumn) {
|
||||
DB::table($table)->where($tenantKeyColumn, $event->tenant->getKey())->delete();
|
||||
DB::table($table)->where($tenantKeyColumn, $event->tenant->getTenantKey())->delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -898,7 +898,7 @@ test('deleting SyncMaster automatically deletes its Syncables', function (bool $
|
|||
'basic pivot' => false,
|
||||
]);
|
||||
|
||||
test('tenant pivot records are deleted along with the tenants to which they belong', function (bool $dbLevelOnCascadeDelete, bool $morphPivot) {
|
||||
test('tenant pivot records are deleted along with the tenants to which they belong to', function (bool $dbLevelOnCascadeDelete, bool $morphPivot) {
|
||||
[$tenant] = createTenantsAndRunMigrations();
|
||||
|
||||
if ($morphPivot) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue