Skip to content

Commit e2aa5f0

Browse files
authored
enable order by date range column (#461)
1 parent 9283cf6 commit e2aa5f0

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/Entries/EntryQueryBuilder.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,23 @@ public function orderBy($column, $direction = 'asc')
7373
$castType = 'date';
7474
}
7575

76+
// take range into account
77+
if ($blueprintField->get('mode') == 'range') {
78+
$actualColumnStartDate = $grammar->wrap($this->column($column).'->start');
79+
$actualColumnEndDate = $grammar->wrap($this->column($column).'->end');
80+
if (str_contains(get_class($grammar), 'SQLiteGrammar')) {
81+
$this->builder
82+
->orderByRaw("datetime({$actualColumnStartDate}) {$direction}")
83+
->orderByRaw("datetime({$actualColumnEndDate}) {$direction}");
84+
} else {
85+
$this->builder
86+
->orderByRaw("cast({$actualColumnStartDate} as {$castType}) {$direction}")
87+
->orderByRaw("cast({$actualColumnEndDate} as {$castType}) {$direction}");
88+
}
89+
90+
return $this;
91+
}
92+
7693
// sqlite casts dates to year, which is pretty unhelpful
7794
if (str_contains(get_class($grammar), 'SQLiteGrammar')) {
7895
$this->builder->orderByRaw("datetime({$actualColumn}) {$direction}");

tests/Data/Entries/EntryQueryBuilderTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,41 @@ public function entries_can_be_ordered_by_an_date_json_field()
797797
$this->assertEquals(['Post 2', 'Post 1', 'Post 3'], $entries->map->title->all());
798798
}
799799

800+
#[Test]
801+
public function entries_can_be_ordered_by_a_datetime_range_json_field()
802+
{
803+
$blueprint = Blueprint::makeFromFields(['date_field' => ['type' => 'date', 'time_enabled' => true, 'mode' => 'range']]);
804+
Blueprint::shouldReceive('in')->with('collections/posts')->andReturn(collect(['posts' => $blueprint]));
805+
806+
Collection::make('posts')->save();
807+
EntryFactory::id('1')->slug('post-1')->collection('posts')->data(['title' => 'Post 1', 'date_field' => ['start' => '2021-06-15 20:31:04', 'end' => '2021-06-15 21:00:00']])->create();
808+
EntryFactory::id('2')->slug('post-2')->collection('posts')->data(['title' => 'Post 2', 'date_field' => ['start' => '2021-01-13 20:31:04', 'end' => '2021-06-16 20:31:04']])->create();
809+
EntryFactory::id('3')->slug('post-3')->collection('posts')->data(['title' => 'Post 3', 'date_field' => ['start' => '2021-11-17 20:31:04', 'end' => '2021-11-17 20:31:04']])->create();
810+
EntryFactory::id('4')->slug('post-4')->collection('posts')->data(['title' => 'Post 4', 'date_field' => ['start' => '2021-06-15 20:31:04', 'end' => '2021-06-15 22:00:00']])->create();
811+
$entries = Entry::query()->where('collection', 'posts')->orderBy('date_field', 'asc')->get();
812+
813+
$this->assertCount(4, $entries);
814+
$this->assertEquals(['Post 2', 'Post 1', 'Post 4', 'Post 3'], $entries->map->title->all());
815+
}
816+
817+
#[Test]
818+
public function entries_can_be_ordered_by_a_date_range_json_field()
819+
{
820+
$blueprint = Blueprint::makeFromFields(['date_field' => ['type' => 'date', 'time_enabled' => false, 'mode' => 'range']]);
821+
Blueprint::shouldReceive('in')->with('collections/posts')->andReturn(collect(['posts' => $blueprint]));
822+
823+
Collection::make('posts')->save();
824+
EntryFactory::id('1')->slug('post-1')->collection('posts')->data(['title' => 'Post 1', 'date_field' => ['start' => '2021-06-15', 'end' => '2021-06-15']])->create();
825+
EntryFactory::id('2')->slug('post-2')->collection('posts')->data(['title' => 'Post 2', 'date_field' => ['start' => '2021-01-13', 'end' => '2021-06-16']])->create();
826+
EntryFactory::id('3')->slug('post-3')->collection('posts')->data(['title' => 'Post 3', 'date_field' => ['start' => '2021-11-17', 'end' => '2021-11-16']])->create();
827+
EntryFactory::id('4')->slug('post-4')->collection('posts')->data(['title' => 'Post 4', 'date_field' => ['start' => '2021-06-15', 'end' => '2021-06-16']])->create();
828+
829+
$entries = Entry::query()->where('collection', 'posts')->orderBy('date_field', 'asc')->get();
830+
831+
$this->assertCount(4, $entries);
832+
$this->assertEquals(['Post 2', 'Post 1', 'Post 4', 'Post 3'], $entries->map->title->all());
833+
}
834+
800835
#[Test]
801836
public function entries_can_be_ordered_by_a_mapped_data_column()
802837
{

0 commit comments

Comments
 (0)