Skip to content

Define descendant relationship on Entry model #455

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 4 commits into
base: 4.x
Choose a base branch
from
Draft
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
14 changes: 14 additions & 0 deletions src/Entries/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Support\Carbon;
use Statamic\Contracts\Entries\Entry as EntryContract;
use Statamic\Entries\Entry as FileEntry;
use Statamic\Entries\EntryCollection;
use Statamic\Facades\Blink;
use Statamic\Facades\Entry as EntryFacade;

Expand Down Expand Up @@ -201,6 +202,19 @@ public function makeLocalization($site)
->data($this->data());
}

public function directDescendants()
{
return Blink::once('entry-descendants-'.$this->id(), function () {
return new EntryCollection($this
->model()
->descendants()
->get()
->map(fn ($model) => static::fromModel($model))
->keyBy
->locale());
});
}

public function getDataColumnMappings(Model $model)
{
if (! config('statamic.eloquent-driver.entries.map_data_to_columns', false)) {
Expand Down
7 changes: 7 additions & 0 deletions src/Entries/EntryModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class EntryModel extends BaseModel

protected $table = 'entries';

protected $with = ['descendants'];

protected $casts = [
'date' => 'datetime',
'data' => 'json',
Expand All @@ -27,6 +29,11 @@ public function origin()
return $this->belongsTo(static::class);
}

public function descendants()
{
return $this->hasMany(static::class, 'origin_id');
}

public function parent()
{
return $this->belongsTo(static::class, 'data->parent');
Expand Down
2 changes: 1 addition & 1 deletion tests/Entries/EntryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public function it_propagates_updating_origin_data_to_descendent_models()
'roo' => 'rar',
]);

$return = $entry->save();
$entry->save();

$this->assertNull($entry->descendants()->get('fr')->model()->data['too'] ?? null);
$this->assertNull($entry->descendants()->get('de')->model()->data['too'] ?? null);
Expand Down