diff --git a/database/migrations/create_entries_table.php.stub b/database/migrations/create_entries_table.php.stub index 850ebb44..fef89461 100644 --- a/database/migrations/create_entries_table.php.stub +++ b/database/migrations/create_entries_table.php.stub @@ -16,6 +16,7 @@ return new class extends Migration { $table->string('slug')->nullable(); $table->string('uri')->nullable()->index(); $table->string('date')->nullable(); + $table->integer('order')->nullable(); $table->string('collection')->index(); $table->json('data'); $table->timestamps(); diff --git a/database/migrations/create_entries_table_with_string_ids.php.stub b/database/migrations/create_entries_table_with_string_ids.php.stub index c14c990c..a93ecebd 100644 --- a/database/migrations/create_entries_table_with_string_ids.php.stub +++ b/database/migrations/create_entries_table_with_string_ids.php.stub @@ -16,6 +16,7 @@ return new class extends Migration { $table->string('slug')->nullable(); $table->string('uri')->nullable()->index(); $table->string('date')->nullable(); + $table->integer('order')->nullable(); $table->string('collection')->index(); $table->json('data'); $table->timestamps(); diff --git a/database/migrations/updates/add_order_to_entries_table.php.stub b/database/migrations/updates/add_order_to_entries_table.php.stub new file mode 100644 index 00000000..520d0766 --- /dev/null +++ b/database/migrations/updates/add_order_to_entries_table.php.stub @@ -0,0 +1,21 @@ +prefix('entries'), function (Blueprint $table) { + $table->integer('order')->after('collection')->nullable(); + }); + } + + public function down() + { + Schema::table($this->prefix('entries'), function (Blueprint $table) { + $table->dropColumn('order'); + }); + } +}; diff --git a/src/Collections/CollectionRepository.php b/src/Collections/CollectionRepository.php index 25cb76d2..4cf6f397 100644 --- a/src/Collections/CollectionRepository.php +++ b/src/Collections/CollectionRepository.php @@ -78,4 +78,9 @@ public static function bindings(): array CollectionContract::class => Collection::class, ]; } + + public function updateEntryOrder(CollectionContract $collection, $ids = null) + { + $collection->queryEntries()->get()->each->save(); + } } diff --git a/src/Commands/ImportCollections.php b/src/Commands/ImportCollections.php index aeea7d47..bb6f5de3 100644 --- a/src/Commands/ImportCollections.php +++ b/src/Commands/ImportCollections.php @@ -2,7 +2,9 @@ namespace Statamic\Eloquent\Commands; +use Closure; use Illuminate\Console\Command; +use Illuminate\Support\Facades\Facade; use Statamic\Console\RunsInPlease; use Statamic\Contracts\Entries\Collection as CollectionContract; use Statamic\Contracts\Entries\CollectionRepository as CollectionRepositoryContract; @@ -40,19 +42,34 @@ class ImportCollections extends Command */ public function handle() { - $this->useDefaultRepositories(); + $this->usingDefaultRepositories(function () { + $this->importCollections(); + }); + + $this->updateEntryOrder(); - $this->importCollections(); + $this->newLine(); + $this->info('Collections imported'); return 0; } - private function useDefaultRepositories() + private function usingDefaultRepositories(Closure $callback) { + $originalRepo = get_class(app()->make(CollectionRepositoryContract::class)); + $originalTreeRepo = get_class(app()->make(CollectionTreeRepositoryContract::class)); + $originalCollection = get_class(app()->make(CollectionContract::class)); + Statamic::repository(CollectionRepositoryContract::class, CollectionRepository::class); Statamic::repository(CollectionTreeRepositoryContract::class, CollectionTreeRepository::class); - app()->bind(CollectionContract::class, StacheCollection::class); + + $callback(); + + Statamic::repository(CollectionRepositoryContract::class, $originalRepo); + Statamic::repository(CollectionTreeRepositoryContract::class, $originalTreeRepo); + app()->bind(CollectionContract::class, $originalCollection); + Facade::clearResolvedInstance(CollectionRepositoryContract::class); } private function importCollections() @@ -74,8 +91,12 @@ private function importCollections() }); } }); + } - $this->newLine(); - $this->info('Collections imported'); + private function updateEntryOrder() + { + $this->withProgressBar(CollectionFacade::all(), function ($collections) { + $collections->updateEntryOrder(); + }); } } diff --git a/src/Entries/Entry.php b/src/Entries/Entry.php index e2fff280..28e92e17 100644 --- a/src/Entries/Entry.php +++ b/src/Entries/Entry.php @@ -53,6 +53,7 @@ public function toModel() 'published' => $this->published(), 'status' => $this->status(), 'updated_at' => $this->lastModified(), + 'order' => $this->order(), ]); } diff --git a/src/Entries/EntryQueryBuilder.php b/src/Entries/EntryQueryBuilder.php index 8e94f2b0..1f8c1d18 100644 --- a/src/Entries/EntryQueryBuilder.php +++ b/src/Entries/EntryQueryBuilder.php @@ -15,7 +15,7 @@ class EntryQueryBuilder extends EloquentQueryBuilder implements QueryBuilder const COLUMNS = [ 'id', 'site', 'origin_id', 'published', 'status', 'slug', 'uri', - 'date', 'collection', 'created_at', 'updated_at', + 'date', 'collection', 'created_at', 'updated_at', 'order', ]; protected function transform($items, $columns = []) diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 60712c8b..d7b05ee7 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -37,6 +37,10 @@ class ServiceProvider extends AddonServiceProvider protected $migrationCount = 0; + protected $updateScripts = [ + \Statamic\Eloquent\Updates\AddOrderToEntriesTable::class, + ]; + public function boot() { parent::boot(); diff --git a/src/Updates/AddOrderToEntriesTable.php b/src/Updates/AddOrderToEntriesTable.php new file mode 100644 index 00000000..ac95478c --- /dev/null +++ b/src/Updates/AddOrderToEntriesTable.php @@ -0,0 +1,26 @@ +isUpdatingTo('1.0.3') + && ! Schema::hasColumn(config('statamic.eloquent-driver.table_prefix', '').'entries', 'order'); + } + + public function update() + { + $source = __DIR__.'/../../database/migrations/updates/add_order_to_entries_table.php.stub'; + $dest = database_path('migrations/'.date('Y_m_d_His').'_add_order_to_entries_table.php'); + + $this->files->copy($source, $dest); + + $this->console()->info('Migration created'); + $this->console()->comment('Remember to run `php artisan migrate` to apply it to your database.'); + } +} diff --git a/tests/Entries/EntryTest.php b/tests/Entries/EntryTest.php index 8285cdb3..a3f315e4 100644 --- a/tests/Entries/EntryTest.php +++ b/tests/Entries/EntryTest.php @@ -45,6 +45,7 @@ public function it_saves_to_entry_model() 'status' => 'draft', 'origin_id' => null, 'id' => null, + 'order' => null, ]); $collection = Collection::make('blog')->title('blog')->routes([