1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-13 02:54:05 +00:00

Delete pivot records when deleting SyncMaster using morph pivot (#46)

* Add and correct pivot record tests

* Implement and test deleting pivot records when using morph pivot

* Use dataset to test deleting pivot records with both MorphTenantPivot and TenantPivot

* Delete extra test, use createTenantsAndRunMigrations() where appropriate

* FIx PHPStan errors

* Revert DB::table() assertion changes

* minor test improvements

---------

Co-authored-by: Samuel Štancl <samuel@archte.ch>
This commit is contained in:
lukinovec 2024-04-22 12:18:15 +02:00 committed by GitHub
parent 4e51cdbacb
commit 77226cf2f4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 55 additions and 15 deletions

View file

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Stancl\Tenancy\ResourceSyncing\Listeners;
use Illuminate\Database\Eloquent\SoftDeletes;
use Stancl\Tenancy\Listeners\QueueableListener;
use Stancl\Tenancy\ResourceSyncing\Events\SyncMasterDeleted;
@ -20,6 +21,12 @@ class DeleteResourcesInTenants extends QueueableListener
tenancy()->runForMultiple($centralResource->tenants()->cursor(), function () use ($centralResource, $forceDelete) {
$this->deleteSyncedResource($centralResource, $forceDelete);
// Delete pivot records if the central resource doesn't use soft deletes
// or the central resource was deleted using forceDelete()
if ($forceDelete || ! in_array(SoftDeletes::class, class_uses_recursive($centralResource::class), true)) {
$centralResource->tenants()->detach(tenant());
}
});
}
}