1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-04 17:04:03 +00:00

Add the option to set values & db name during tenant creation

This commit is contained in:
Samuel Štancl 2019-08-11 21:58:16 +02:00
parent 490c776bc8
commit 1320780915
3 changed files with 48 additions and 1 deletions

View file

@ -217,6 +217,32 @@ You can use the `tenancy()` and `tenant()` helpers to resolve `Stancl\Tenancy\Te
]
```
You can also put data into the storage during the tenant creation process:
```php
>>> tenant()->create('dev.localhost', [
'plan' => 'basic'
])
=> [
"uuid" => "49670df0-1a87-11e9-b7ba-cf5353777957",
"domain" => "dev.localhost",
"plan" => "basic",
]
```
If you want to specify the tenant's database name, set the `tenancy.database_name_key` configuration key to the name of the key that is used to specify the database name in the tenant storage. You must use a name that you won't use for storing other data, so it's recommended to avoid names like `database` and use names like `_stancl_tenancy_database_name` instead. Then just give the key a value during the tenant creation process:
```php
>>> tenant()->create('example.com', [
'_stancl_tenancy_database_name' => 'example_com'
])
=> [
"uuid" => "49670df0-1a87-11e9-b7ba-cf5353777957",
"domain" => "example.com",
"_stancl_tenancy_database_name" => "example_com",
]
```
When you create a new tenant, you can [migrate](#tenant-migrations) their database like this:
```php

View file

@ -70,7 +70,14 @@ class TenantManager
return $tenant;
}
public function create(string $domain = null): array
/**
* Create a tenant.
*
* @param string $domain
* @param array $data
* @return array
*/
public function create(string $domain = null, array $data = []): array
{
$domain = $domain ?: $this->currentDomain();
@ -79,6 +86,13 @@ class TenantManager
}
$tenant = $this->jsonDecodeArrayValues($this->storage->createTenant($domain, (string) \Webpatser\Uuid\Uuid::generate(1, $domain)));
if ($data) {
$this->put($data, null, $tenant['uuid']);
$tenant = array_merge($tenant, $data);
}
$this->database->create($this->getDatabaseName($tenant));
return $tenant;
@ -168,6 +182,12 @@ class TenantManager
{
$tenant = $tenant ?: $this->tenant;
if ($key = $this->app['config']['tenancy.database_name_key']) {
if (isset($tenant[$key])) {
return $tenant[$key];
}
}
return $this->app['config']['tenancy.database.prefix'] . $tenant['uuid'] . $this->app['config']['tenancy.database.suffix'];
}

View file

@ -43,4 +43,5 @@ return [
],
'queue_database_creation' => false,
'queue_database_deletion' => false,
'database_name_key' => null,
];