1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 15:34:03 +00:00

BelongsToParentModel

This commit is contained in:
Samuel Štancl 2020-05-14 04:11:44 +02:00
parent 28019f4528
commit 42160aa93c
4 changed files with 94 additions and 2 deletions

View file

@ -0,0 +1,15 @@
<?php
namespace Stancl\Tenancy\Database\Concerns;
use Stancl\Tenancy\Database\ParentModelScope;
trait BelongsToPrimaryModel
{
abstract public function getParentRelationshipName(): string;
public static function bootBelongsToPrimaryModel()
{
static::addGlobalScope(new ParentModelScope);
}
}

View file

@ -19,7 +19,7 @@ trait BelongsToTenant
public static function bootBelongsToTenant() public static function bootBelongsToTenant()
{ {
static::addGlobalScope(new TenantScope(BelongsToTenant::$tenantIdColumn)); static::addGlobalScope(new TenantScope);
static::creating(function ($model) { static::creating(function ($model) {
if (! $model->getAttribute(BelongsToTenant::$tenantIdColumn) && ! $model->relationLoaded('tenant')) { if (! $model->getAttribute(BelongsToTenant::$tenantIdColumn) && ! $model->relationLoaded('tenant')) {

View file

@ -0,0 +1,26 @@
<?php
namespace Stancl\Tenancy\Database;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
class ParentModelScope implements Scope
{
public function apply(Builder $builder, Model $model)
{
if (! tenancy()->initialized) {
return;
}
$builder->whereHas($builder->getModel()->getParentRelationshipName());
}
public function extend(Builder $builder)
{
$builder->macro('withoutParentModel', function (Builder $builder) {
return $builder->withoutGlobalScope($this);
});
}
}

View file

@ -7,6 +7,7 @@ use Illuminate\Database\QueryException;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema; use Illuminate\Support\Facades\Schema;
use Stancl\Tenancy\Database\Concerns\BelongsToPrimaryModel;
use Stancl\Tenancy\Database\Concerns\BelongsToTenant; use Stancl\Tenancy\Database\Concerns\BelongsToTenant;
use Stancl\Tenancy\Tests\Etc\Tenant; use Stancl\Tenancy\Tests\Etc\Tenant;
@ -129,10 +130,43 @@ class SingleDatabaseTenancyTest extends TestCase
// We're in acme context // We're in acme context
$this->assertSame('acme', tenant('id')); $this->assertSame('acme', tenant('id'));
// There is no way to scope this 🤷‍♂
$this->assertSame(2, Comment::count()); $this->assertSame(2, Comment::count());
} }
/** @test */
public function secondary_models_ARE_scoped_to_the_current_tenant_when_accessed_directly_AND_PARENT_RELATIONSHIP_TRAIT_IS_USED()
{
$acme = Tenant::create([
'id' => 'acme',
]);
$acme->run(function () {
$post = Post::create(['text' => 'Foo']);
$post->scoped_comments()->create(['text' => 'Comment Text']);
$this->assertSame(1, Post::count());
$this->assertSame(1, ScopedComment::count());
});
$foobar = Tenant::create([
'id' => 'foobar',
]);
$foobar->run(function () {
$this->assertSame(0, Post::count());
$this->assertSame(0, ScopedComment::count());
$post = Post::create(['text' => 'Bar']);
$post->scoped_comments()->create(['text' => 'Comment Text 2']);
$this->assertSame(1, Post::count());
$this->assertSame(1, ScopedComment::count());
});
// Global context
$this->assertSame(2, ScopedComment::count());
}
/** @test */ /** @test */
public function secondary_models_are_NOT_scoped_in_the_central_context() public function secondary_models_are_NOT_scoped_in_the_central_context()
{ {
@ -252,6 +286,11 @@ class Post extends Model
{ {
return $this->hasMany(Comment::class); return $this->hasMany(Comment::class);
} }
public function scoped_comments()
{
return $this->hasMany(Comment::class);
}
} }
class Comment extends Model class Comment extends Model
@ -265,6 +304,18 @@ class Comment extends Model
} }
} }
class ScopedComment extends Comment
{
use BelongsToPrimaryModel;
protected $table = 'comments';
public function getParentRelationshipName(): string
{
return 'post';
}
}
class GlobalResource extends Model class GlobalResource extends Model
{ {
protected $guarded = []; protected $guarded = [];