mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 11:14:03 +00:00
separate pivot model for each approach
This commit is contained in:
parent
dfe1fcecb5
commit
b08961150c
4 changed files with 31 additions and 10 deletions
|
|
@ -8,6 +8,7 @@ use Illuminate\Database\Eloquent\Relations\MorphToMany;
|
|||
use Stancl\Tenancy\Contracts\Syncable;
|
||||
use Stancl\Tenancy\Contracts\UniqueIdentifierGenerator;
|
||||
use Stancl\Tenancy\Database\Models\Tenant;
|
||||
use Stancl\Tenancy\Database\Models\TenantMorphPivot;
|
||||
use Stancl\Tenancy\Database\Models\TenantPivot;
|
||||
use Stancl\Tenancy\Events\SyncedResourceSaved;
|
||||
|
||||
|
|
@ -50,7 +51,7 @@ trait ResourceSyncing
|
|||
public function tenants(): MorphToMany
|
||||
{
|
||||
return $this->morphToMany($this->getResourceTenantModelName(), 'tenant_resources', 'tenant_resources', 'resource_global_id', 'tenant_id', 'global_id')
|
||||
->using(TenantPivot::class);
|
||||
->using(TenantMorphPivot::class);
|
||||
}
|
||||
|
||||
public function getResourceTenantModelName(): string // todo better name
|
||||
|
|
|
|||
22
src/Database/Models/TenantMorphPivot.php
Normal file
22
src/Database/Models/TenantMorphPivot.php
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Database\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Relations\MorphPivot;
|
||||
use Stancl\Tenancy\Contracts\Syncable;
|
||||
|
||||
class TenantMorphPivot extends MorphPivot
|
||||
{
|
||||
public static function booted(): void
|
||||
{
|
||||
static::saved(function (self $pivot) {
|
||||
$parent = $pivot->pivotParent;
|
||||
|
||||
if ($parent instanceof Syncable && $parent->shouldSync()) {
|
||||
$parent->triggerSyncEvent();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -4,10 +4,10 @@ declare(strict_types=1);
|
|||
|
||||
namespace Stancl\Tenancy\Database\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Relations\MorphPivot;
|
||||
use Illuminate\Database\Eloquent\Relations\Pivot;
|
||||
use Stancl\Tenancy\Contracts\Syncable;
|
||||
|
||||
class TenantPivot extends MorphPivot
|
||||
class TenantPivot extends Pivot
|
||||
{
|
||||
public static function booted(): void
|
||||
{
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@ declare(strict_types=1);
|
|||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphToMany;
|
||||
use Illuminate\Events\CallQueuedListener;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Illuminate\Support\Facades\Queue;
|
||||
|
|
@ -824,10 +823,9 @@ function migrateUsersTableForTenants(): void
|
|||
|
||||
class ResourceTenant extends Tenant
|
||||
{
|
||||
public function users(): MorphToMany
|
||||
public function users(): BelongsToMany
|
||||
{
|
||||
return $this->morphedByMany(CentralUserForPolymorphic::class, 'tenant_resources', 'tenant_resources', 'tenant_id', 'resource_global_id', 'id', 'global_id')
|
||||
->using(TenantPivot::class);
|
||||
return $this->belongsToMany(CentralUser::class, 'tenant_users', 'tenant_id', 'global_user_id', 'id', 'global_id');
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -841,10 +839,10 @@ class CentralUser extends Model implements SyncMaster
|
|||
|
||||
public $table = 'users';
|
||||
|
||||
// override method to provide different tenant
|
||||
public function getResourceTenantModelName(): string
|
||||
public function tenants(): BelongsToMany
|
||||
{
|
||||
return ResourceTenant::class;
|
||||
return $this->belongsToMany(ResourceTenant::class, 'tenant_users', 'global_user_id', 'tenant_id', 'global_id')
|
||||
->using(TenantPivot::class);
|
||||
}
|
||||
|
||||
public function getTenantModelName(): string
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue