Skip to content

Commit

Permalink
Orderby new features (#744)
Browse files Browse the repository at this point in the history
* Update README.md

* added secondary param to orderby relation
  • Loading branch information
bsormagec authored Jan 8, 2021
1 parent ecb9a5d commit a1a2e20
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,18 @@ ORDER BY posts.title
...
```

`http://prettus.local/users?orderBy=posts:custom_id,other_id|posts.title&sortedBy=desc`

Query will have something like this

```sql
...
INNER JOIN posts ON users.custom_id = posts.other_id
...
ORDER BY posts.title
...
```

Sorting multiple columns same sortedBy

`http://prettus.local/users?orderBy=name;created_at&sortedBy=desc`
Expand Down
11 changes: 9 additions & 2 deletions src/Prettus/Repository/Criteria/RequestCriteria.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,16 @@ protected function parserFieldsOrderBy($model, $orderBy, $sortedBy)
$sortColumn = $split[1];

$split = explode(':', $sortTable);
if(count($split) > 1) {
$localKey = '.id';
if (count($split) > 1) {
$sortTable = $split[0];

$commaExp = explode(',', $split[1]);
$keyName = $table.'.'.$split[1];
if (count($commaExp) > 1) {
$keyName = $table.'.'.$commaExp[0];
$localKey = '.'.$commaExp[1];
}
} else {
/*
* If you do not define which column to use as a joining column on current table, it will
Expand All @@ -206,7 +213,7 @@ protected function parserFieldsOrderBy($model, $orderBy, $sortedBy)
}

$model = $model
->leftJoin($sortTable, $keyName, '=', $sortTable.'.id')
->leftJoin($sortTable, $keyName, '=', $sortTable.$localKey)
->orderBy($sortColumn, $sortedBy)
->addSelect($table.'.*');
} else {
Expand Down

0 comments on commit a1a2e20

Please sign in to comment.