-
Notifications
You must be signed in to change notification settings - Fork 92
Fix ordering assets by integers, floats, and date fields #487
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
Merged
Merged
Changes from 16 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
97b0163
Add tests
duncanmcclean 28607bf
Unrelated, but tweak method names in EntryQueryBuilderTest
duncanmcclean a82cb19
Fix asset sorting
duncanmcclean 16b042b
wip
duncanmcclean 54ecad3
wip
duncanmcclean 90528d0
Fix styling
duncanmcclean 157da20
wip
duncanmcclean 99c75d1
Always cast default meta data
duncanmcclean 6ff98e3
Extract some of the logic into a trait
duncanmcclean 1a52794
Fix styling
duncanmcclean 0cd816b
simplify
duncanmcclean 613fa27
Fix styling
duncanmcclean 1dba7ab
Rename some things. It's not always gonna be "meta"
duncanmcclean 267b2a5
wip
duncanmcclean d3337f4
revert changes to test names
duncanmcclean d1deb20
don't want any changes for this file in the diff
duncanmcclean f26b6dc
`column` doesn't need to be in the trait
duncanmcclean File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| <?php | ||
|
|
||
| namespace Statamic\Eloquent; | ||
|
|
||
| use Illuminate\Support\Collection; | ||
| use Illuminate\Support\Str; | ||
| use Statamic\Fields\Field; | ||
|
|
||
| trait QueriesJsonColumns | ||
| { | ||
| public function orderBy($column, $direction = 'asc') | ||
| { | ||
| $actualColumn = $this->column($column); | ||
|
|
||
| if ( | ||
| Str::contains($actualColumn, ['data->', 'meta->']) | ||
| && $jsonCast = $this->getJsonCasts()->get($column) | ||
| ) { | ||
| $grammar = $this->builder->getConnection()->getQueryGrammar(); | ||
| $wrappedColumn = $grammar->wrap($actualColumn); | ||
|
|
||
| if (Str::contains($jsonCast, 'range_')) { | ||
| $jsonCast = Str::after($jsonCast, 'range_'); | ||
|
|
||
| $wrappedStartDateColumn = $grammar->wrap("{$actualColumn}->start"); | ||
| $wrappedEndDateColumn = $grammar->wrap("{$actualColumn}->end"); | ||
|
|
||
| if (str_contains(get_class($grammar), 'SQLiteGrammar')) { | ||
| $this->builder | ||
| ->orderByRaw("datetime({$wrappedStartDateColumn}) {$direction}") | ||
| ->orderByRaw("datetime({$wrappedEndDateColumn}) {$direction}"); | ||
| } else { | ||
| $this->builder | ||
| ->orderByRaw("cast({$wrappedStartDateColumn} as {$jsonCast}) {$direction}") | ||
| ->orderByRaw("cast({$wrappedEndDateColumn} as {$jsonCast}) {$direction}"); | ||
| } | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| // SQLite casts dates to year, which is pretty unhelpful. | ||
| if ( | ||
| in_array($jsonCast, ['date', 'datetime']) | ||
| && Str::contains(get_class($grammar), 'SQLiteGrammar') | ||
| ) { | ||
| $this->builder->orderByRaw("datetime({$wrappedColumn}) {$direction}"); | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| $this->builder->orderByRaw("cast({$wrappedColumn} as {$jsonCast}) {$direction}"); | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| parent::orderBy($column, $direction); | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| abstract protected function column($column); | ||
|
|
||
| abstract protected function getJsonCasts(): Collection; | ||
|
|
||
| protected function toCast(Field $field): string | ||
| { | ||
| $cast = match (true) { | ||
| $field->type() === 'float' => 'float', | ||
| $field->type() === 'integer' => 'float', // A bit sneaky, but MySQL doesn't support casting as integer, it wants unsigned. | ||
| $field->type() === 'date' => $field->get('time_enabled') ? 'datetime' : 'date', | ||
| default => null, | ||
| }; | ||
|
|
||
| // Date Ranges are dealt with a little bit differently. | ||
| if ($field->type() === 'date' && $field->get('mode') === 'range') { | ||
| $cast = "range_{$cast}"; | ||
| } | ||
|
|
||
| return $cast; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.