mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 13:54:03 +00:00
Use getTenantKey() instead of ->id references
This commit is contained in:
parent
d7536ce0af
commit
ac58f6992b
8 changed files with 15 additions and 15 deletions
|
|
@ -43,7 +43,7 @@ final class MigrateFresh extends Command
|
||||||
|
|
||||||
$this->info('Migrating.');
|
$this->info('Migrating.');
|
||||||
$this->callSilent('tenants:migrate', [
|
$this->callSilent('tenants:migrate', [
|
||||||
'--tenants' => [$tenant->id],
|
'--tenants' => [$tenant->getTenantKey()],
|
||||||
'--force' => true,
|
'--force' => true,
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ interface Tenant
|
||||||
public function getTenantKeyName(): string;
|
public function getTenantKeyName(): string;
|
||||||
|
|
||||||
/** Get the value of the key used for identifying the tenant. */
|
/** Get the value of the key used for identifying the tenant. */
|
||||||
public function getTenantKey(): string;
|
public function getTenantKey(): ?string;
|
||||||
|
|
||||||
/** Get the value of an internal key. */
|
/** Get the value of an internal key. */
|
||||||
public function getInternal(string $key);
|
public function getInternal(string $key);
|
||||||
|
|
|
||||||
|
|
@ -9,8 +9,8 @@ trait GeneratesIds
|
||||||
public static function bootGeneratesIds()
|
public static function bootGeneratesIds()
|
||||||
{
|
{
|
||||||
static::creating(function (self $model) {
|
static::creating(function (self $model) {
|
||||||
if (! $model->id && app()->bound(UniqueIdentifierGenerator::class)) {
|
if (! $model->getTenantKey() && app()->bound(UniqueIdentifierGenerator::class)) {
|
||||||
$model->id = app(UniqueIdentifierGenerator::class)->generate($model);
|
$model->setAttribute($model->getTenantKeyName(), app(UniqueIdentifierGenerator::class)->generate($model));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,7 @@ class Tenant extends Model implements Contracts\Tenant
|
||||||
return 'id';
|
return 'id';
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getTenantKey(): string
|
public function getTenantKey(): ?string
|
||||||
{
|
{
|
||||||
return $this->getAttribute($this->getTenantKeyName());
|
return $this->getAttribute($this->getTenantKeyName());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -37,7 +37,7 @@ class DatabaseConfig
|
||||||
};
|
};
|
||||||
|
|
||||||
static::$databaseNameGenerator = static::$databaseNameGenerator ?? function (Tenant $tenant) {
|
static::$databaseNameGenerator = static::$databaseNameGenerator ?? function (Tenant $tenant) {
|
||||||
return config('tenancy.database.prefix') . $tenant->id . config('tenancy.database.suffix');
|
return config('tenancy.database.prefix') . $tenant->getTenantKey() . config('tenancy.database.suffix');
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -10,16 +10,16 @@ use Illuminate\Foundation\Bus\Dispatchable;
|
||||||
use Illuminate\Queue\InteractsWithQueue;
|
use Illuminate\Queue\InteractsWithQueue;
|
||||||
use Illuminate\Queue\SerializesModels;
|
use Illuminate\Queue\SerializesModels;
|
||||||
use Illuminate\Support\Facades\Artisan;
|
use Illuminate\Support\Facades\Artisan;
|
||||||
use Stancl\Tenancy\Database\Models\Tenant;
|
use Stancl\Tenancy\Contracts\TenantWithDatabase;
|
||||||
|
|
||||||
class MigrateDatabase implements ShouldQueue
|
class MigrateDatabase implements ShouldQueue
|
||||||
{
|
{
|
||||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||||
|
|
||||||
/** @var Tenant */
|
/** @var TenantWithDatabase */
|
||||||
protected $tenant;
|
protected $tenant;
|
||||||
|
|
||||||
public function __construct(Tenant $tenant)
|
public function __construct(TenantWithDatabase $tenant)
|
||||||
{
|
{
|
||||||
$this->tenant = $tenant;
|
$this->tenant = $tenant;
|
||||||
}
|
}
|
||||||
|
|
@ -32,7 +32,7 @@ class MigrateDatabase implements ShouldQueue
|
||||||
public function handle()
|
public function handle()
|
||||||
{
|
{
|
||||||
Artisan::call('tenants:migrate', [
|
Artisan::call('tenants:migrate', [
|
||||||
'--tenants' => [$this->tenant->id],
|
'--tenants' => [$this->tenant->getTenantKey()],
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,16 +10,16 @@ use Illuminate\Foundation\Bus\Dispatchable;
|
||||||
use Illuminate\Queue\InteractsWithQueue;
|
use Illuminate\Queue\InteractsWithQueue;
|
||||||
use Illuminate\Queue\SerializesModels;
|
use Illuminate\Queue\SerializesModels;
|
||||||
use Illuminate\Support\Facades\Artisan;
|
use Illuminate\Support\Facades\Artisan;
|
||||||
use Stancl\Tenancy\Database\Models\Tenant;
|
use Stancl\Tenancy\Contracts\TenantWithDatabase;
|
||||||
|
|
||||||
class SeedDatabase implements ShouldQueue
|
class SeedDatabase implements ShouldQueue
|
||||||
{
|
{
|
||||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||||
|
|
||||||
/** @var Tenant */
|
/** @var TenantWithDatabase */
|
||||||
protected $tenant;
|
protected $tenant;
|
||||||
|
|
||||||
public function __construct(Tenant $tenant)
|
public function __construct(TenantWithDatabase $tenant)
|
||||||
{
|
{
|
||||||
$this->tenant = $tenant;
|
$this->tenant = $tenant;
|
||||||
}
|
}
|
||||||
|
|
@ -32,7 +32,7 @@ class SeedDatabase implements ShouldQueue
|
||||||
public function handle()
|
public function handle()
|
||||||
{
|
{
|
||||||
Artisan::call('tenants:seed', [
|
Artisan::call('tenants:seed', [
|
||||||
'--tenants' => [$this->tenant->id],
|
'--tenants' => [$this->tenant->getTenantKey()],
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -131,7 +131,7 @@ class CommandsTest extends TestCase
|
||||||
/** @test */
|
/** @test */
|
||||||
public function run_commands_works()
|
public function run_commands_works()
|
||||||
{
|
{
|
||||||
$id = Tenant::create()->id;
|
$id = Tenant::create()->getTenantKey();
|
||||||
|
|
||||||
Artisan::call('tenants:migrate', ['--tenants' => [$id]]);
|
Artisan::call('tenants:migrate', ['--tenants' => [$id]]);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue