1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 12:54:05 +00:00
This commit is contained in:
Samuel Štancl 2022-10-01 17:59:33 +02:00
parent 065b029f48
commit 24146b26e2
7 changed files with 17 additions and 13 deletions

View file

@ -215,4 +215,12 @@ return [
'--class' => 'DatabaseSeeder', // root seeder class '--class' => 'DatabaseSeeder', // root seeder class
// '--force' => true, // '--force' => true,
], ],
/**
* Single-database tenancy config.
*/
'single_db' => [
/** The name of the column used by models with the BelongsToTenant trait. */
'tenant_id_column' => 'tenant_id',
],
]; ];

View file

@ -12,11 +12,9 @@ use Stancl\Tenancy\Database\TenantScope;
*/ */
trait BelongsToTenant trait BelongsToTenant
{ {
public static $tenantIdColumn = 'tenant_id';
public function tenant() public function tenant()
{ {
return $this->belongsTo(config('tenancy.tenant_model'), BelongsToTenant::$tenantIdColumn); return $this->belongsTo(config('tenancy.tenant_model'), config('tenancy.single_db.tenant_id_column'));
} }
public static function bootBelongsToTenant(): void public static function bootBelongsToTenant(): void
@ -24,9 +22,9 @@ trait BelongsToTenant
static::addGlobalScope(new TenantScope); static::addGlobalScope(new TenantScope);
static::creating(function ($model) { static::creating(function ($model) {
if (! $model->getAttribute(BelongsToTenant::$tenantIdColumn) && ! $model->relationLoaded('tenant')) { if (! $model->getAttribute(config('tenancy.single_db.tenant_id_column')) && ! $model->relationLoaded('tenant')) {
if (tenancy()->initialized) { if (tenancy()->initialized) {
$model->setAttribute(BelongsToTenant::$tenantIdColumn, tenant()->getTenantKey()); $model->setAttribute(config('tenancy.single_db.tenant_id_column'), tenant()->getTenantKey());
$model->setRelation('tenant', tenant()); $model->setRelation('tenant', tenant());
} }
} }

View file

@ -11,11 +11,11 @@ trait HasScopedValidationRules
{ {
public function unique($table, $column = 'NULL') public function unique($table, $column = 'NULL')
{ {
return (new Unique($table, $column))->where(BelongsToTenant::$tenantIdColumn, $this->getTenantKey()); return (new Unique($table, $column))->where(config('tenancy.single_db.tenant_id_column'), $this->getTenantKey());
} }
public function exists($table, $column = 'NULL') public function exists($table, $column = 'NULL')
{ {
return (new Exists($table, $column))->where(BelongsToTenant::$tenantIdColumn, $this->getTenantKey()); return (new Exists($table, $column))->where(config('tenancy.single_db.tenant_id_column'), $this->getTenantKey());
} }
} }

View file

@ -11,7 +11,7 @@ use Stancl\Tenancy\Resolvers\Contracts\CachedTenantResolver;
trait InvalidatesResolverCache trait InvalidatesResolverCache
{ {
/** @var array<class-string<CachedTenantResolver>> */ /** @var array<class-string<CachedTenantResolver>> */
public static $resolvers = [ public static $resolvers = [ // todo@deprecated, move this to a config key? related to a todo in InvalidatesTenantsResolverCache
Resolvers\DomainTenantResolver::class, Resolvers\DomainTenantResolver::class,
Resolvers\PathTenantResolver::class, Resolvers\PathTenantResolver::class,
Resolvers\RequestDataTenantResolver::class, Resolvers\RequestDataTenantResolver::class,

View file

@ -17,7 +17,7 @@ class TenantScope implements Scope
return; return;
} }
$builder->where($model->qualifyColumn(BelongsToTenant::$tenantIdColumn), tenant()->getTenantKey()); $builder->where($model->qualifyColumn(config('tenancy.single_db.tenant_id_column')), tenant()->getTenantKey());
} }
public function extend(Builder $builder): void public function extend(Builder $builder): void

View file

@ -13,8 +13,6 @@ use Stancl\Tenancy\Database\Concerns\HasScopedValidationRules;
use Stancl\Tenancy\Tests\Etc\Tenant as TestTenant; use Stancl\Tenancy\Tests\Etc\Tenant as TestTenant;
beforeEach(function () { beforeEach(function () {
BelongsToTenant::$tenantIdColumn = 'tenant_id';
Schema::create('posts', function (Blueprint $table) { Schema::create('posts', function (Blueprint $table) {
$table->increments('id'); $table->increments('id');
$table->string('text'); $table->string('text');
@ -144,7 +142,7 @@ test('tenant id is not auto added when creating primary resources in central con
}); });
test('tenant id column name can be customized', function () { test('tenant id column name can be customized', function () {
BelongsToTenant::$tenantIdColumn = 'team_id'; config(['tenancy.single_db.tenant_id_column' => 'team_id']);
Schema::drop('comments'); Schema::drop('comments');
Schema::drop('posts'); Schema::drop('posts');