Skip to content

Commit dcd6ed3

Browse files
authored
Handle whereIn() in new ordering clause logic (#188)
1 parent 0ea4ed5 commit dcd6ed3

File tree

1 file changed

+41
-34
lines changed

1 file changed

+41
-34
lines changed

src/Entries/EntryQueryBuilder.php

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -27,44 +27,51 @@ public function orderBy($column, $direction = 'asc')
2727
$wheres = collect($this->builder->getQuery()->wheres);
2828

2929
if ($wheres->where('column', 'collection')->count() == 1) {
30-
if ($collection = Collection::find($wheres->firstWhere('column', 'collection')['value'])) {
31-
$blueprintField = $collection->entryBlueprints()
32-
->flatMap(function ($blueprint) {
33-
return $blueprint->fields()
34-
->all()
35-
->filter(function ($field) {
36-
return in_array($field->type(), ['float', 'integer', 'date']);
37-
});
38-
})
39-
->filter()
40-
->get($column);
41-
42-
if ($blueprintField) {
43-
$castType = '';
44-
$fieldType = $blueprintField->type();
45-
46-
$grammar = $this->builder->getConnection()->getQueryGrammar();
47-
$actualColumn = $grammar->wrap($actualColumn);
48-
49-
if (in_array($fieldType, ['float'])) {
50-
$castType = 'float';
51-
} elseif (in_array($fieldType, ['integer'])) {
52-
$castType = 'float'; // bit sneaky but mysql doesnt support casting as integer, it wants unsigned
53-
} elseif (in_array($fieldType, ['date'])) {
54-
$castType = 'date';
55-
56-
// sqlite casts dates to year, which is pretty unhelpful
57-
if (str_contains(get_class($grammar), 'SQLiteGrammar')) {
58-
$this->builder->orderByRaw("datetime({$actualColumn}) {$direction}");
30+
$collectionWhere = $wheres->firstWhere('column', 'collection');
31+
if (isset($collectionWhere['values']) && count($collectionWhere['values']) == 1) {
32+
$collectionWhere['value'] = $collectionWhere['values'][0];
33+
}
5934

60-
return $this;
35+
if (isset($collectionWhere['value'])) {
36+
if ($collection = Collection::find($collectionWhere['value'])) {
37+
$blueprintField = $collection->entryBlueprints()
38+
->flatMap(function ($blueprint) {
39+
return $blueprint->fields()
40+
->all()
41+
->filter(function ($field) {
42+
return in_array($field->type(), ['float', 'integer', 'date']);
43+
});
44+
})
45+
->filter()
46+
->get($column);
47+
48+
if ($blueprintField) {
49+
$castType = '';
50+
$fieldType = $blueprintField->type();
51+
52+
$grammar = $this->builder->getConnection()->getQueryGrammar();
53+
$actualColumn = $grammar->wrap($actualColumn);
54+
55+
if (in_array($fieldType, ['float'])) {
56+
$castType = 'float';
57+
} elseif (in_array($fieldType, ['integer'])) {
58+
$castType = 'float'; // bit sneaky but mysql doesnt support casting as integer, it wants unsigned
59+
} elseif (in_array($fieldType, ['date'])) {
60+
$castType = 'date';
61+
62+
// sqlite casts dates to year, which is pretty unhelpful
63+
if (str_contains(get_class($grammar), 'SQLiteGrammar')) {
64+
$this->builder->orderByRaw("datetime({$actualColumn}) {$direction}");
65+
66+
return $this;
67+
}
6168
}
62-
}
6369

64-
if ($castType) {
65-
$this->builder->orderByRaw("cast({$actualColumn} as {$castType}) {$direction}");
70+
if ($castType) {
71+
$this->builder->orderByRaw("cast({$actualColumn} as {$castType}) {$direction}");
6672

67-
return $this;
73+
return $this;
74+
}
6875
}
6976
}
7077
}

0 commit comments

Comments
 (0)