Skip to content

Commit

Permalink
Fix #97
Browse files Browse the repository at this point in the history
  • Loading branch information
GinoPane committed Apr 23, 2021
1 parent 0440a4c commit ed2db06
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ Types of changes

## [Unreleased]

## 3.0.3 - 2021-04-23

### Fixed
* Fixed a group by clause when sorting by a particular field on ModelAbstract

## 3.0.2 - 2021-04-11

### Fixed
Expand Down Expand Up @@ -282,4 +287,4 @@ Types of changes
### Features
* The initial release of Blog Taxonomy.

[Unreleased]: https://github.com/GinoPane/oc-blogtaxonomy-plugin/compare/v3.0.2...HEAD
[Unreleased]: https://github.com/GinoPane/oc-blogtaxonomy-plugin/compare/v3.0.3...HEAD
23 changes: 16 additions & 7 deletions models/ModelAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function scopeListFrontend(Builder $query, array $options = [])
{
$this->withRelation($query, $options);

$this->queryOrderBy($query, $options);
$sortField = $this->queryOrderBy($query, $options);

$this->queryDisplayEmpty($query, $options);

Expand All @@ -68,7 +68,7 @@ public function scopeListFrontend(Builder $query, array $options = [])
// GROUP BY is required for SQLite to deal with HAVING
// We use it for all connections just to keep implementation
// independent from the connection being used
$this->queryGroupBy($query);
$this->queryGroupBy($query, $sortField);

return $query->get();
}
Expand Down Expand Up @@ -144,9 +144,9 @@ private function queryLimit(Builder $query, array $options)
* @param Builder $query
* @param array $options
*
* @return void
* @return string|null
*/
private function queryOrderBy(Builder $query, array $options)
private function queryOrderBy(Builder $query, array $options): ?string
{
if (!empty($options['sort']) && \array_key_exists($options['sort'], static::$sortingOptions)) {
if ($options['sort'] === 'random') {
Expand All @@ -155,8 +155,12 @@ private function queryOrderBy(Builder $query, array $options)
list($sortField, $sortDirection) = explode(' ', $options['sort']);

$query->orderBy($sortField, $sortDirection);

return $sortField;
}
}

return null;
}

/**
Expand Down Expand Up @@ -191,11 +195,16 @@ protected function withRelation(Builder $query, array $options)
}

/**
* @param Builder $query
* @param Builder $query
* @param string|null $sortField
*/
private function queryGroupBy(Builder $query)
private function queryGroupBy(Builder $query, ?string $sortField = null)
{
$query->groupBy('id');
if ($sortField !== null) {
$query->groupBy('id', $sortField);
} else {
$query->groupBy('id');
}
}

/**
Expand Down
3 changes: 3 additions & 0 deletions updates/version.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
3.0.3:
- Fixed a group by clause when sorting by a particular field on ModelAbstract

3.0.2:
- Try to fix an issue with unsupported json data type on old DB servers

Expand Down

0 comments on commit ed2db06

Please sign in to comment.