Skip to content

Commit eaa33ec

Browse files
Fix taxonomy null caching and ensure taxonomy column in term queries (#534)
* Fix taxonomy null caching and ensure taxonomy column in term queries * missing imports --------- Co-authored-by: Bart Waardenburg <bartwaardenburg@gmail.com>
1 parent 8411773 commit eaa33ec

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

src/Taxonomies/TaxonomyRepository.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,18 @@ public function all(): Collection
4040

4141
public function findByHandle($handle): ?TaxonomyContract
4242
{
43-
$taxonomyModel = Blink::once("eloquent-taxonomies-{$handle}", function () use ($handle) {
43+
$blinkKey = "eloquent-taxonomies-{$handle}";
44+
$taxonomyModel = Blink::once($blinkKey, function () use ($handle) {
4445
return app('statamic.eloquent.taxonomies.model')::whereHandle($handle)->first();
4546
});
4647

47-
return $taxonomyModel instanceof Model ? app(TaxonomyContract::class)->fromModel($taxonomyModel) : null;
48+
if (! $taxonomyModel instanceof Model) {
49+
Blink::forget($blinkKey);
50+
51+
return null;
52+
}
53+
54+
return app(TaxonomyContract::class)->fromModel($taxonomyModel);
4855
}
4956

5057
public function findByUri(string $uri, ?string $site = null): ?Taxonomy

src/Taxonomies/TermQueryBuilder.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,11 @@ public function get($columns = ['*'])
174174
{
175175
$this->applyCollectionAndTaxonomyWheres();
176176

177+
// Ensure 'taxonomy' is always selected as it's required for Term::fromModel().
178+
if (! in_array('*', $columns) && ! in_array('taxonomy', $columns)) {
179+
$columns[] = 'taxonomy';
180+
}
181+
177182
$items = parent::get($columns);
178183

179184
// If a single collection has been queried, we'll supply it to the terms so
@@ -212,6 +217,11 @@ public function paginate($perPage = null, $columns = [], $pageName = 'page', $pa
212217
{
213218
$this->applyCollectionAndTaxonomyWheres();
214219

220+
// Ensure 'taxonomy' is always selected as it's required for Term::fromModel().
221+
if (! in_array('*', $columns) && ! in_array('taxonomy', $columns)) {
222+
$columns[] = 'taxonomy';
223+
}
224+
215225
return parent::paginate($perPage, $columns, $pageName, $page);
216226
}
217227

tests/Terms/TermTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
use Statamic\Eloquent\Entries\Entry;
1010
use Statamic\Eloquent\Taxonomies\Taxonomy;
1111
use Statamic\Eloquent\Taxonomies\TermModel;
12+
use Statamic\Facades\Blink;
1213
use Statamic\Facades\Collection;
1314
use Statamic\Facades\Stache;
15+
use Statamic\Facades\Taxonomy as TaxonomyFacade;
1416
use Statamic\Facades\Term as TermFacade;
1517
use Statamic\Statamic;
1618
use Statamic\Testing\Concerns\PreventsSavingStacheItemsToDisk;
@@ -117,4 +119,41 @@ public function it_applies_taxonomy_wheres_using_pluck_count_and_get()
117119
$this->assertSame(1, $taxonomy->queryTerms()->count());
118120
$this->assertSame($term->slug(), $taxonomy->queryTerms()->get()->pluck('slug')->first());
119121
}
122+
123+
#[Test]
124+
public function it_does_not_cache_null_taxonomy_lookups()
125+
{
126+
$taxonomy = TaxonomyFacade::findByHandle('future');
127+
$this->assertNull($taxonomy);
128+
129+
// Create taxonomy directly in DB, bypassing TaxonomyRepository::save().
130+
$modelClass = app('statamic.eloquent.taxonomies.model');
131+
$modelClass::create([
132+
'handle' => 'future',
133+
'title' => 'Future Taxonomy',
134+
'sites' => ['en'],
135+
'settings' => [],
136+
]);
137+
138+
$taxonomy = TaxonomyFacade::findByHandle('future');
139+
140+
$this->assertNotNull($taxonomy);
141+
$this->assertEquals('future', $taxonomy->handle());
142+
}
143+
144+
#[Test]
145+
public function it_queries_terms_with_taxonomy_available()
146+
{
147+
Taxonomy::make('tags')->title('Tags')->save();
148+
TermFacade::make('test-tag')->taxonomy('tags')->data(['title' => 'Test Tag'])->save();
149+
150+
Blink::flush();
151+
152+
$terms = TermFacade::query()->get();
153+
154+
$this->assertCount(1, $terms);
155+
$this->assertEquals('test-tag', $terms->first()->slug());
156+
$this->assertNotNull($terms->first()->taxonomy());
157+
$this->assertEquals('tags', $terms->first()->taxonomy()->handle());
158+
}
120159
}

0 commit comments

Comments
 (0)