From aa7b3955ff454f3dc159515b86910cf382eebca8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Svale=20Fossa=CC=8Askaret?= Date: Mon, 8 Oct 2018 10:08:07 +0200 Subject: [PATCH 1/3] Update GROUP BY clause in SELECT queries Ensure they are compatible with sql_mode=only_full_group_by (default from MySQL 5.7.5+) --- src/services/Similar.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/services/Similar.php b/src/services/Similar.php index 944e226..9044f4a 100644 --- a/src/services/Similar.php +++ b/src/services/Similar.php @@ -130,13 +130,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; } From 31b6283d602333c7cf7d166bfa23ec9e43934a95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Svale=20Fossa=CC=8Askaret?= Date: Mon, 8 Oct 2018 10:08:59 +0200 Subject: [PATCH 2/3] Only try to fetch elements if SQL query returns an `id` --- src/services/Similar.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/services/Similar.php b/src/services/Similar.php index 9044f4a..8663045 100644 --- a/src/services/Similar.php +++ b/src/services/Similar.php @@ -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; + } } } From 090ea7497589d65a92092b2ef950f91dac7db4fb Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Mon, 8 Oct 2018 10:40:34 -0400 Subject: [PATCH 3/3] Version 1.0.4 Signed-off-by: Andrew Welch --- CHANGELOG.md | 5 ++++- composer.json | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 95269f7..8305170 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/composer.json b/composer.json index 6910094..35aae50 100644 --- a/composer.json +++ b/composer.json @@ -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",