Skip to content

Commit 9e66aa3

Browse files
committed
(fix) support many to one order
1 parent 514c5c0 commit 9e66aa3

2 files changed

Lines changed: 25 additions & 5 deletions

File tree

src/Persistence/Model/Replaceable.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,30 @@ public function sorter(): array
4444
return [static::CREATED_AT => 'ASC'];
4545
}
4646

47+
/**
48+
* @param array|null $sorter
49+
*
50+
* @return array
51+
*/
52+
public function parseSorter(?array $sorter = null): array
53+
{
54+
if (!is_array($sorter)) {
55+
return $this->sorter();
56+
}
57+
58+
$manyToOne = $this->manyToOne();
59+
$parsed = [];
60+
foreach ($sorter as $field => $type) {
61+
if (isset($manyToOne[$field])) {
62+
$column = $manyToOne[$field];
63+
$parsed[$column] = $type;
64+
continue;
65+
}
66+
$parsed[$field] = $type;
67+
}
68+
return $parsed;
69+
}
70+
4771
/**
4872
* @param bool $detailed
4973
*

src/Persistence/Repository/Search.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,7 @@ public function search(array $options = [], $trash = false): array
2929
$filters = $options['filters'] ?? [];
3030
$offset = (int)($options['offset'] ?? 0);
3131
$limit = (int)($options['limit'] ?? 25);
32-
$sorter = $options['sorter'] ?? null;
33-
34-
if (!is_array($sorter)) {
35-
$sorter = $this->model->sorter();
36-
}
32+
$sorter = $this->model->parseSorter($options['sorter'] ?? null);
3733

3834
return $this->find($filters, $sorter, $offset, $limit, $trash)->toArray();
3935
}

0 commit comments

Comments
 (0)