Skip to content
Merged
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
1 change: 1 addition & 0 deletions database/migrations/create_entries_table.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
21 changes: 21 additions & 0 deletions database/migrations/updates/add_order_to_entries_table.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Statamic\Eloquent\Database\BaseMigration as Migration;

return new class extends Migration {
public function up()
{
Schema::table($this->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');
});
}
};
5 changes: 5 additions & 0 deletions src/Collections/CollectionRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
33 changes: 27 additions & 6 deletions src/Commands/ImportCollections.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand All @@ -74,8 +91,12 @@ private function importCollections()
});
}
});
}

$this->newLine();
$this->info('Collections imported');
private function updateEntryOrder()
{
$this->withProgressBar(CollectionFacade::all(), function ($collections) {
$collections->updateEntryOrder();
});
}
}
1 change: 1 addition & 0 deletions src/Entries/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public function toModel()
'published' => $this->published(),
'status' => $this->status(),
'updated_at' => $this->lastModified(),
'order' => $this->order(),
]);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Entries/EntryQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [])
Expand Down
4 changes: 4 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ class ServiceProvider extends AddonServiceProvider

protected $migrationCount = 0;

protected $updateScripts = [
\Statamic\Eloquent\Updates\AddOrderToEntriesTable::class,
];

public function boot()
{
parent::boot();
Expand Down
26 changes: 26 additions & 0 deletions src/Updates/AddOrderToEntriesTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Statamic\Eloquent\Updates;

use Illuminate\Support\Facades\Schema;
use Statamic\UpdateScripts\UpdateScript;

class AddOrderToEntriesTable extends UpdateScript
{
public function shouldUpdate($newVersion, $oldVersion)
{
return $this->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.');
}
}
1 change: 1 addition & 0 deletions tests/Entries/EntryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down