Skip to content

Commit

Permalink
Merge branch 'release/1.1.3' into v1
Browse files Browse the repository at this point in the history
  • Loading branch information
khalwat committed Mar 27, 2021
2 parents c6b1fc3 + ba9a475 commit e6a6e7f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
# Similar Changelog

## 1.1.3 - 2021.03.27
### Fixed
* Fixed an issue with using `RAND()` in the sort criteria (https://github.com/nystudio107/craft-similar/issues/32)

## 1.1.2 - 2021.03.23
### Fixed
* Fixed an SQL error related to even stricter GROUP BY rules.
* Fixed an error where using a tag field that had no matches on other elements could return a random set of entries. [\#37](https://github.com/nystudio107/craft-similar/issues/31)
* Fixed an error where using a tag field that had no matches on other elements could return a random set of entries. (https://github.com/nystudio107/craft-similar/issues/31)

## 1.1.1 - 2021.03.11
### Fixed
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "nystudio107/craft-similar",
"description": "Similar for Craft lets you find elements, Entries, Categories, Commerce Products, etc, that are similar, based on... other related elements.",
"type": "craft-plugin",
"version": "1.1.2",
"version": "1.1.3",
"keywords": [
"craft",
"cms",
Expand Down
12 changes: 9 additions & 3 deletions src/services/Similar.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public function find($data)
if (empty($results)) {
return [];
}

// Fetch all the elements in one fell swoop, including any preset eager-loaded conditions
$query = $this->getElementQuery($elementClass, $criteria);

Expand Down Expand Up @@ -190,15 +190,21 @@ protected function eventAfterPrepareHandler(CancelableEvent $event)
$query = $event->sender;
// Add in the `count` param so we know how many were fetched
$query->query->addSelect(['COUNT(*) as count']);
$query->query->orderBy('count DESC, '.str_replace('`', '', $this->preOrder));
if (is_array($this->preOrder)) {
$query->query->orderBy(array_merge([
'count' => 'DESC',
], $this->preOrder));
} elseif (is_string($this->preOrder)) {
$query->query->orderBy('count DESC, '.str_replace('`', '', $this->preOrder));
}
$query->query->groupBy(['relations.sourceId', 'elements.id', 'elements_sites.siteId']);

$query->query->andWhere(['in', 'relations.targetId', $this->targetElements]);
$query->subQuery->limit(null); // inner limit to null -> fetch all possible entries, sort them afterwards
$query->query->limit($this->limit); // or whatever limit is set

$query->subQuery->groupBy(['elements.id', 'content.id', 'elements_sites.id']);

if ($query instanceof EntryQuery) {
$query->subQuery->addGroupBy(['entries.postDate']);
}
Expand Down

0 comments on commit e6a6e7f

Please sign in to comment.