Skip to content

Adds EntryRespository#findByIds() #417

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

Draft
wants to merge 1 commit into
base: 4.x
Choose a base branch
from
Draft
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
25 changes: 25 additions & 0 deletions src/Entries/EntryRepository.php
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@
use Statamic\Contracts\Entries\QueryBuilder;
use Statamic\Eloquent\Jobs\UpdateCollectionEntryOrder;
use Statamic\Eloquent\Jobs\UpdateCollectionEntryParent;
use Statamic\Entries\EntryCollection;
use Statamic\Facades\Blink;
use Statamic\Stache\Repositories\EntryRepository as StacheRepository;

@@ -51,6 +52,30 @@ public function findByUri(string $uri, ?string $site = null): ?EntryContract
return $this->substitutionsById[$item->id()] ?? $item;
}

public function findByIds($ids): EntryCollection
{
$cached = collect($ids)->flip()->map(fn ($_, $id) => Blink::get("eloquent-entry-{$id}"));
$missingIds = $cached->reject()->keys();

$missingById = $this->query()
->whereIn('id', $missingIds)
->get()
->keyBy->id();

$missingById->each(function ($entry, $id) {
Blink::put("eloquent-entry-{$id}", $entry);
});

$items = $cached
->map(fn ($entry, $id) => $entry ?? $missingById->get($id))
->filter()
->values();

$this->applySubstitutions($items);

return EntryCollection::make($items);
}

public function save($entry)
{
$model = $entry->toModel();
72 changes: 72 additions & 0 deletions tests/Entries/EntryRepositoryTest.php
Original file line number Diff line number Diff line change
@@ -4,11 +4,14 @@

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Event;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Eloquent\Entries\Entry;
use Statamic\Eloquent\Entries\EntryModel;
use Statamic\Eloquent\Entries\EntryRepository;
use Statamic\Entries\EntryCollection;
use Statamic\Events\CollectionTreeSaved;
use Statamic\Facades\Blink;
use Statamic\Facades\Collection;
use Statamic\Stache\Stache;
use Tests\TestCase;
@@ -234,4 +237,73 @@ public function it_updates_the_parents_of_specific_entries_in_a_collection()
4 => null,
], EntryModel::all()->mapWithKeys(fn ($e) => [$e->id => $e->data['parent'] ?? null])->all());
}

#[Test, Group('EntryRepository#findByIds')]
public function it_gets_entries_by_ids()
{
$collection = Collection::make('pages')->routes('{slug}')->save();
$expected = collect([
(new Entry)->collection($collection)->slug('foo'),
(new Entry)->collection($collection)->slug('bar'),
])->each->save();

$actual = (new EntryRepository(new Stache))->findByIds($expected->map->id());

$this->assertInstanceOf(EntryCollection::class, $actual);
$this->assertEquals($expected->map->id()->all(), $actual->map->id()->all());
}

#[Test, Group('EntryRepository#findByIds')]
public function it_loads_entries_from_database_given_partial_cache_when_finding_by_ids()
{
$collection = Collection::make('pages')->routes('{slug}')->save();
$expected = collect([
(new Entry)->collection($collection)->slug('foo'),
(new Entry)->collection($collection)->slug('bar'),
]);

$expected->first()->save();
Blink::flush();
$expected->last()->save();

$actual = (new EntryRepository(new Stache))->findByIds($expected->map->id());

$this->assertNotNull($expected->first()->id());
$this->assertNotSame($expected->first(), $actual->first());
$this->assertEquals($expected->first()->id(), $actual->first()->id());
$this->assertNotNull($actual->last());
$this->assertSame($expected->last(), $actual->last());
}

#[Test, Group('EntryRepository#findByIds')]
public function it_returns_entries_in_exact_order_when_finding_by_ids()
{
$collection = Collection::make('pages')->routes('{slug}')->save();
$entries = collect([
(new Entry)->collection($collection)->slug('foo'),
(new Entry)->collection($collection)->slug('bar'),
(new Entry)->collection($collection)->slug('baz'),
])->each->save();

Blink::flush();

$expected = collect([2, 0, 1])->map(fn ($index) => $entries[$index]->id())->all();
$actual = (new EntryRepository(new Stache))->findByIds($expected);

$this->assertEquals($expected, $actual->map->id()->all());
}

#[Test, Group('EntryRepository#findByIds')]
public function it_skips_missing_entires_when_finding_by_ids()
{
$collection = Collection::make('pages')->routes('{slug}')->save();
$expected = tap((new Entry)->collection($collection)->slug('foo'))->save();

$actual = (new EntryRepository(new Stache))->findByIds([
$expected->id(),
'missing',
]);

$this->assertEquals([$expected->id()], $actual->map->id()->all());
}
}