mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 20:34:03 +00:00
Fix #998, properly replace ALL tenant_id literals
This commit is contained in:
parent
8a00a105d0
commit
2a39b0526a
11 changed files with 35 additions and 22 deletions
|
|
@ -6,6 +6,7 @@ namespace Stancl\Tenancy\Database\Concerns;
|
|||
|
||||
use Stancl\Tenancy\Contracts\Tenant;
|
||||
use Stancl\Tenancy\Database\TenantScope;
|
||||
use Stancl\Tenancy\Tenancy;
|
||||
|
||||
/**
|
||||
* @property-read Tenant $tenant
|
||||
|
|
@ -14,12 +15,7 @@ trait BelongsToTenant
|
|||
{
|
||||
public function tenant()
|
||||
{
|
||||
return $this->belongsTo(config('tenancy.models.tenant'), static::tenantIdColumn());
|
||||
}
|
||||
|
||||
public static function tenantIdColumn(): string
|
||||
{
|
||||
return config('tenancy.models.tenant_key_column');
|
||||
return $this->belongsTo(config('tenancy.models.tenant'), Tenancy::tenantKeyColumn());
|
||||
}
|
||||
|
||||
public static function bootBelongsToTenant(): void
|
||||
|
|
@ -27,9 +23,9 @@ trait BelongsToTenant
|
|||
static::addGlobalScope(new TenantScope);
|
||||
|
||||
static::creating(function ($model) {
|
||||
if (! $model->getAttribute(static::tenantIdColumn()) && ! $model->relationLoaded('tenant')) {
|
||||
if (! $model->getAttribute(Tenancy::tenantKeyColumn()) && ! $model->relationLoaded('tenant')) {
|
||||
if (tenancy()->initialized) {
|
||||
$model->setAttribute(static::tenantIdColumn(), tenant()->getTenantKey());
|
||||
$model->setAttribute(Tenancy::tenantKeyColumn(), tenant()->getTenantKey());
|
||||
$model->setRelation('tenant', tenant());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ declare(strict_types=1);
|
|||
namespace Stancl\Tenancy\Database\Concerns;
|
||||
|
||||
use Stancl\Tenancy\Contracts\Domain;
|
||||
use Stancl\Tenancy\Tenancy;
|
||||
|
||||
/**
|
||||
* @property-read Domain[]|\Illuminate\Database\Eloquent\Collection $domains
|
||||
|
|
@ -15,7 +16,7 @@ trait HasDomains
|
|||
{
|
||||
public function domains()
|
||||
{
|
||||
return $this->hasMany(config('tenancy.models.domain'), 'tenant_id');
|
||||
return $this->hasMany(config('tenancy.models.domain'), Tenancy::tenantKeyColumn());
|
||||
}
|
||||
|
||||
public function createDomain($data): Domain
|
||||
|
|
|
|||
|
|
@ -6,16 +6,17 @@ namespace Stancl\Tenancy\Database\Concerns;
|
|||
|
||||
use Illuminate\Validation\Rules\Exists;
|
||||
use Illuminate\Validation\Rules\Unique;
|
||||
use Stancl\Tenancy\Tenancy;
|
||||
|
||||
trait HasScopedValidationRules
|
||||
{
|
||||
public function unique($table, $column = 'NULL')
|
||||
{
|
||||
return (new Unique($table, $column))->where(BelongsToTenant::tenantIdColumn(), $this->getTenantKey());
|
||||
return (new Unique($table, $column))->where(Tenancy::tenantKeyColumn(), $this->getTenantKey());
|
||||
}
|
||||
|
||||
public function exists($table, $column = 'NULL')
|
||||
{
|
||||
return (new Exists($table, $column))->where(BelongsToTenant::tenantIdColumn(), $this->getTenantKey());
|
||||
return (new Exists($table, $column))->where(Tenancy::tenantKeyColumn(), $this->getTenantKey());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ namespace Stancl\Tenancy\Database;
|
|||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Scope;
|
||||
use Stancl\Tenancy\Database\Concerns\BelongsToTenant;
|
||||
use Stancl\Tenancy\Tenancy;
|
||||
|
||||
class TenantScope implements Scope
|
||||
{
|
||||
|
|
@ -17,7 +17,7 @@ class TenantScope implements Scope
|
|||
return;
|
||||
}
|
||||
|
||||
$builder->where($model->qualifyColumn(BelongsToTenant::tenantIdColumn()), tenant()->getTenantKey());
|
||||
$builder->where($model->qualifyColumn(Tenancy::tenantKeyColumn()), tenant()->getTenantKey());
|
||||
}
|
||||
|
||||
public function extend(Builder $builder): void
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue