Skip to content

Commit

Permalink
Merge branch 'release/1.0.4' into v1
Browse files Browse the repository at this point in the history
  • Loading branch information
khalwat committed Oct 8, 2018
2 parents 92dfb33 + 090ea74 commit f463c19
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Similar Changelog

All notable changes to this project will be documented in this file.
## 1.0.4 - 2018-10-08
### Changed
* Only try to fetch elements if SQL query returns an `id`
* Update GROUP BY clause in SELECT queries to ensure they are compatible with `sql_mode=only_full_group_by`

## 1.0.3 - 2018-08-04
### Changed
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.0.3",
"version": "1.0.4",
"keywords": [
"craft",
"cms",
Expand Down
18 changes: 10 additions & 8 deletions src/services/Similar.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,14 @@ public function find($data)
$elements = Craft::$app->getElements();
$models = [];
foreach ($results as $config) {
$model = $elements->getElementById($config['id'], $elementClass, $config['siteId']);
if ($model) {
// The `count` property is added dynamically by our CountBehavior behavior
/** @noinspection PhpUndefinedFieldInspection */
$model->count = $config['count'];
$models[] = $model;
if($config['id'] && $config['siteId']) {
$model = $elements->getElementById($config['id'], $elementClass, $config['siteId']);
if ($model) {
// The `count` property is added dynamically by our CountBehavior behavior
/** @noinspection PhpUndefinedFieldInspection */
$model->count = $config['count'];
$models[] = $model;
}
}
}

Expand All @@ -130,13 +132,13 @@ protected function eventAfterPrepareHandler(CancelableEvent $event)
// 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));
$query->query->groupBy('{{%relations}}.sourceId');
$query->query->groupBy(['{{%relations}}.sourceId', 'elements.id']);

$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');
$query->subQuery->groupBy(['structureelements.lft', 'elements.id']);
$event->isValid = true;
}

Expand Down

0 comments on commit f463c19

Please sign in to comment.