Skip to content

Commit 12c8d80

Browse files
Fix URI and related issues (#317)
Co-authored-by: Ryan Mitchell <[email protected]>
1 parent d8527e6 commit 12c8d80

File tree

9 files changed

+351
-77
lines changed

9 files changed

+351
-77
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
},
2626
"require": {
2727
"php": "^8.1",
28-
"statamic/cms": "^5.12"
28+
"statamic/cms": "^5.14"
2929
},
3030
"require-dev": {
3131
"doctrine/dbal": "^3.8",

src/Collections/CollectionRepository.php

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,11 @@
44

55
use Illuminate\Support\Collection as IlluminateCollection;
66
use Statamic\Contracts\Entries\Collection as CollectionContract;
7-
use Statamic\Eloquent\Jobs\UpdateCollectionEntryOrder;
87
use Statamic\Facades\Blink;
98
use Statamic\Stache\Repositories\CollectionRepository as StacheRepository;
109

1110
class CollectionRepository extends StacheRepository
1211
{
13-
public function updateEntryUris($collection, $ids = null)
14-
{
15-
$query = $collection->queryEntries()
16-
->when($ids, fn ($query) => $query->whereIn('id', $ids))
17-
->get()
18-
->each(function ($entry) {
19-
app('statamic.eloquent.entries.model')::find($entry->id())->update(['uri' => $entry->uri()]);
20-
});
21-
}
22-
2312
public function all(): IlluminateCollection
2413
{
2514
return Blink::once('eloquent-collections', function () {
@@ -76,22 +65,4 @@ public static function bindings(): array
7665
CollectionContract::class => Collection::class,
7766
];
7867
}
79-
80-
public function updateEntryOrder(CollectionContract $collection, $ids = null)
81-
{
82-
$query = $collection->queryEntries()
83-
->when($ids, fn ($query) => $query->whereIn('id', $ids))
84-
->get(['id'])
85-
->each(function ($entry) {
86-
$dispatch = UpdateCollectionEntryOrder::dispatch($entry->id());
87-
88-
$connection = config('statamic.eloquent-driver.collections.update_entry_order_connection', 'default');
89-
90-
if ($connection != 'default') {
91-
$dispatch->onConnection($connection);
92-
}
93-
94-
$dispatch->onQueue(config('statamic.eloquent-driver.collections.update_entry_order_queue', 'default'));
95-
});
96-
}
9768
}

src/Entries/Entry.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,17 @@ public static function makeModelFromContract(EntryContract $source)
100100

101101
$dataMappings = (new self)->getDataColumnMappings(new $class);
102102

103+
$attributes = [];
104+
105+
if ($id = $source->id()) {
106+
$attributes['id'] = $id;
107+
108+
// Ensure that when calling $source->uri() that it doesn't use the cached value.
109+
Blink::store('entry-uris')->forget($source->id());
110+
}
111+
103112
$attributes = [
113+
...$attributes,
104114
'origin_id' => $origin?->id(),
105115
'site' => $source->locale(),
106116
'slug' => $source->slug(),
@@ -118,10 +128,6 @@ public static function makeModelFromContract(EntryContract $source)
118128
$attributes[$key] = $data->get($key);
119129
}
120130

121-
if ($id = $source->id()) {
122-
$attributes['id'] = $id;
123-
}
124-
125131
return $class::findOrNew($id)->fill($attributes);
126132
}
127133

src/Entries/EntryRepository.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use Statamic\Contracts\Entries\Entry as EntryContract;
66
use Statamic\Contracts\Entries\QueryBuilder;
7+
use Statamic\Eloquent\Jobs\UpdateCollectionEntryOrder;
8+
use Statamic\Eloquent\Jobs\UpdateCollectionEntryParent;
79
use Statamic\Facades\Blink;
810
use Statamic\Stache\Repositories\EntryRepository as StacheRepository;
911

@@ -67,4 +69,50 @@ public function delete($entry)
6769

6870
$entry->model()->delete();
6971
}
72+
73+
public function updateUris($collection, $ids = null)
74+
{
75+
$ids = collect($ids);
76+
77+
$collection->queryEntries()
78+
->when($ids->isNotEmpty(), fn ($query) => $query->whereIn('id', $ids))
79+
->get()
80+
->each(fn ($entry) => $entry->model()->update(['uri' => $entry->uri()]));
81+
}
82+
83+
public function updateOrders($collection, $ids = null)
84+
{
85+
$collection->queryEntries()
86+
->when($ids, fn ($query) => $query->whereIn('id', $ids))
87+
->get(['id'])
88+
->each(function ($entry) {
89+
$dispatch = UpdateCollectionEntryOrder::dispatch($entry->id());
90+
91+
$connection = config('statamic.eloquent-driver.collections.update_entry_order_connection', 'default');
92+
93+
if ($connection != 'default') {
94+
$dispatch->onConnection($connection);
95+
}
96+
97+
$dispatch->onQueue(config('statamic.eloquent-driver.collections.update_entry_order_queue', 'default'));
98+
});
99+
}
100+
101+
public function updateParents($collection, $ids = null)
102+
{
103+
$collection->queryEntries()
104+
->when($ids, fn ($query) => $query->whereIn('id', $ids))
105+
->get(['id'])
106+
->each(function ($entry) {
107+
$dispatch = UpdateCollectionEntryParent::dispatch($entry->id());
108+
109+
$connection = config('statamic.eloquent-driver.collections.update_entry_parent_connection', 'default');
110+
111+
if ($connection != 'default') {
112+
$dispatch->onConnection($connection);
113+
}
114+
115+
$dispatch->onQueue(config('statamic.eloquent-driver.collections.update_entry_parent_queue', 'default'));
116+
});
117+
}
70118
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Statamic\Eloquent\Jobs;
4+
5+
use Illuminate\Bus\Queueable;
6+
use Illuminate\Contracts\Queue\ShouldQueue;
7+
use Illuminate\Foundation\Bus\Dispatchable;
8+
use Illuminate\Queue\InteractsWithQueue;
9+
use Statamic\Facades\Entry;
10+
11+
class UpdateCollectionEntryParent implements ShouldQueue
12+
{
13+
use Dispatchable, InteractsWithQueue, Queueable;
14+
15+
public $entryId;
16+
17+
public function __construct($entryId)
18+
{
19+
$this->entryId = $entryId;
20+
}
21+
22+
public function handle()
23+
{
24+
if ($entry = Entry::find($this->entryId)) {
25+
$entry->save();
26+
}
27+
}
28+
}

src/Listeners/UpdateStructuredEntryOrder.php

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/ServiceProvider.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
use Statamic\Eloquent\Forms\SubmissionRepository;
3232
use Statamic\Eloquent\Globals\GlobalRepository;
3333
use Statamic\Eloquent\Globals\GlobalVariablesRepository;
34-
use Statamic\Eloquent\Listeners\UpdateStructuredEntryOrder;
3534
use Statamic\Eloquent\Revisions\RevisionRepository;
3635
use Statamic\Eloquent\Structures\CollectionTreeRepository;
3736
use Statamic\Eloquent\Structures\NavigationRepository;
@@ -40,7 +39,6 @@
4039
use Statamic\Eloquent\Taxonomies\TermQueryBuilder;
4140
use Statamic\Eloquent\Taxonomies\TermRepository;
4241
use Statamic\Eloquent\Tokens\TokenRepository;
43-
use Statamic\Events\CollectionTreeSaved;
4442
use Statamic\Providers\AddonServiceProvider;
4543
use Statamic\Statamic;
4644

@@ -61,12 +59,6 @@ class ServiceProvider extends AddonServiceProvider
6159
\Statamic\Eloquent\Updates\ChangeFormSubmissionsIdType::class,
6260
];
6361

64-
protected $listen = [
65-
CollectionTreeSaved::class => [
66-
UpdateStructuredEntryOrder::class,
67-
],
68-
];
69-
7062
public function boot()
7163
{
7264
parent::boot();

0 commit comments

Comments
 (0)