Skip to content

Add support for withoutGlobalScopes #424

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/CacheKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,23 @@ class CacheKey
protected $macroKey;
protected $model;
protected $query;
protected $withoutGlobalScopes = [];
protected $withoutAllGlobalScopes = false;

public function __construct(
array $eagerLoad,
$model,
$query,
$macroKey
$macroKey,
array $withoutGlobalScopes,
$withoutAllGlobalScopes
) {
$this->eagerLoad = $eagerLoad;
$this->macroKey = $macroKey;
$this->model = $model;
$this->query = $query;
$this->withoutGlobalScopes = $withoutGlobalScopes;
$this->withoutAllGlobalScopes = $withoutAllGlobalScopes;
}

public function make(
Expand Down Expand Up @@ -57,6 +63,14 @@ protected function getBindingsSlug() : string
return '';
}

if ($this->withoutAllGlobalScopes) {
return Arr::query($this->model->query()->withoutGlobalScopes()->getBindings());
}

if (count($this->withoutGlobalScopes) > 0) {
return Arr::query($this->model->query()->withoutGlobalScopes($this->withoutGlobalScopes)->getBindings());
}

return Arr::query($this->model->query()->getBindings());
}

Expand Down
21 changes: 21 additions & 0 deletions src/Traits/BuilderCaching.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,25 @@ public function truncate()

return parent::truncate();
}

public function withoutGlobalScope($scope)
{
array_push($this->withoutGlobalScopes, $scope);

return parent::withoutGlobalScope($scope);
}

public function withoutGlobalScopes(array $scopes = null)
{
if ($scopes != null) {
$this->withoutGlobalScopes = $scopes;
}

if ($scopes == null || ($scopes != null && count($scopes) == 0)) {
$this->withoutAllGlobalScopes = true;
}

return parent::withoutGlobalScopes($scopes);
}

}
7 changes: 5 additions & 2 deletions src/Traits/Caching.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ trait Caching
protected $isCachable = true;
protected $scopesAreApplied = false;
protected $macroKey = "";
protected $withoutGlobalScopes = [];
protected $withoutAllGlobalScopes = false;

public function __call($method, $parameters)
{
Expand Down Expand Up @@ -45,12 +47,13 @@ protected function applyScopesToInstance()
{
if (! property_exists($this, "scopes")
|| $this->scopesAreApplied
|| $this->withoutAllGlobalScopes
) {
return;
}

foreach ($this->scopes as $identifier => $scope) {
if (! isset($this->scopes[$identifier])) {
if (! isset($this->scopes[$identifier]) || isset($this->withoutGlobalScopes[$identifier])) {
continue;
}

Expand Down Expand Up @@ -166,7 +169,7 @@ protected function makeCacheKey(
$query = $this->query->getQuery();
}

return (new CacheKey($eagerLoad, $model, $query, $this->macroKey))
return (new CacheKey($eagerLoad, $model, $query, $this->macroKey, $this->withoutGlobalScopes, $this->withoutAllGlobalScopes))
->make($columns, $idColumn, $keyDifferentiator);
}

Expand Down
77 changes: 77 additions & 0 deletions tests/Integration/CachedBuilder/ScopeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,83 @@ public function testGlobalScopesWhenSwitchingContextUsingGetMethod()
$this->assertEquals("B", $authorsB->first());
}

public function testGlobalScopesAreNotCachedWhenUsingWithoutGlobalScopes()
{
$user = factory(User::class)->create(["name" => "Abernathy Kings"]);
$this->actingAs($user);
$author = factory(UncachedAuthor::class, 1)
->create(['name' => 'Alois'])
->first();
$authors = (new AuthorBeginsWithScoped)
->withoutGlobalScopes()
->get();
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthorbeginswithscoped");
$tags = ["genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthorbeginswithscoped"];

$cachedResults = $this->cache()
->tags($tags)
->get($key)['value'];
$liveResults = (new UncachedAuthor)
->nameStartsWith("A")
->get();

$this->assertTrue($authors->contains($author));
$this->assertTrue($cachedResults->contains($author));
$this->assertTrue($liveResults->contains($author));
}

public function testWithoutGlobalScopes()
{
factory(Author::class, 200)->create();
$user = factory(User::class)->create(["name" => "Andrew Junior"]);
$this->actingAs($user);
$authorsA = (new AuthorBeginsWithScoped)
->withoutGlobalScopes()
->get()
->map(function ($author) {
return (new Str)->substr($author->name, 0, 1);
})
->unique();
$user = factory(User::class)->create(["name" => "Barry Barry Barry"]);
$this->actingAs($user);
$authorsB = (new AuthorBeginsWithScoped)
->withoutGlobalScopes(['GeneaLabs\LaravelModelCaching\Tests\Fixtures\Scopes\NameBeginsWith'])
->get()
->map(function ($author) {
return (new Str)->substr($author->name, 0, 1);
})
->unique();

$this->assertGreaterThan(1, count($authorsA));
$this->assertGreaterThan(1, count($authorsB));
}

public function testWithoutGlobalScope()
{
factory(Author::class, 200)->create();
$user = factory(User::class)->create(["name" => "Andrew Junior"]);
$this->actingAs($user);
$authorsA = (new AuthorBeginsWithScoped)
->withoutGlobalScope('GeneaLabs\LaravelModelCaching\Tests\Fixtures\Scopes\NameBeginsWith')
->get()
->map(function ($author) {
return (new Str)->substr($author->name, 0, 1);
})
->unique();
$user = factory(User::class)->create(["name" => "Barry Barry Barry"]);
$this->actingAs($user);
$authorsB = (new AuthorBeginsWithScoped)
->withoutGlobalScope('GeneaLabs\LaravelModelCaching\Tests\Fixtures\Scopes\NameBeginsWith')
->get()
->map(function ($author) {
return (new Str)->substr($author->name, 0, 1);
})
->unique();

$this->assertGreaterThan(1, count($authorsA));
$this->assertGreaterThan(1, count($authorsB));
}

public function testLocalScopesInRelationship()
{
$first = "A";
Expand Down