mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-13 01:14:04 +00:00
Domain model & resolver test
This commit is contained in:
parent
08ed5084d5
commit
e1a4054743
11 changed files with 158 additions and 8 deletions
|
|
@ -3,19 +3,42 @@
|
|||
namespace Stancl\Tenancy\Database\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Stancl\Tenancy\Contracts\Tenant;
|
||||
use Stancl\Tenancy\Events\DomainCreated;
|
||||
use Stancl\Tenancy\Events\DomainDeleted;
|
||||
use Stancl\Tenancy\Events\DomainSaved;
|
||||
use Stancl\Tenancy\Events\DomainUpdated;
|
||||
use Stancl\Tenancy\Exceptions\DomainsOccupiedByOtherTenantException;
|
||||
|
||||
/**
|
||||
* @property string $domain
|
||||
* @property string $tenant_id
|
||||
*
|
||||
* @property-read Tenant $tenant
|
||||
*/
|
||||
class Domain extends Model
|
||||
{
|
||||
public function tenant()
|
||||
public $guarded = [];
|
||||
|
||||
public static function booted()
|
||||
{
|
||||
return $this->belongsTo(Tenant::class);
|
||||
$ensureDomainIsNotOccupied = function (Domain $self) {
|
||||
if ($domain = Domain::where('domain', $self->domain)->first()) {
|
||||
if ($domain->getKey() !== $self->getKey()) {
|
||||
throw new DomainsOccupiedByOtherTenantException;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
static::saving($ensureDomainIsNotOccupied);
|
||||
}
|
||||
|
||||
protected $dispatchEvents = [
|
||||
public function tenant()
|
||||
{
|
||||
return $this->belongsTo(config('tenancy.tenant_model'));
|
||||
}
|
||||
|
||||
public $dispatchEvents = [
|
||||
'saved' => DomainSaved::class,
|
||||
'created' => DomainCreated::class,
|
||||
'updated' => DomainUpdated::class,
|
||||
|
|
|
|||
|
|
@ -5,9 +5,11 @@ namespace Stancl\Tenancy\Database\Models;
|
|||
use Illuminate\Database\Eloquent\Model;
|
||||
use Stancl\Tenancy\DatabaseConfig;
|
||||
use Stancl\Tenancy\Events;
|
||||
use Stancl\Tenancy\Contracts;
|
||||
|
||||
// todo use a contract
|
||||
class Tenant extends Model
|
||||
// todo @property
|
||||
class Tenant extends Model implements Contracts\Tenant
|
||||
{
|
||||
use Concerns\CentralConnection, Concerns\HasADataColumn, Concerns\GeneratesIds, Concerns\HasADataColumn {
|
||||
Concerns\HasADataColumn::getCasts as dataColumnCasts;
|
||||
|
|
@ -80,7 +82,7 @@ class Tenant extends Model
|
|||
return $result;
|
||||
}
|
||||
|
||||
protected $dispatchesEvents = [
|
||||
public $dispatchesEvents = [
|
||||
'saved' => Events\TenantSaved::class,
|
||||
'created' => Events\TenantCreated::class,
|
||||
'updated' => Events\TenantUpdated::class,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue