From 3c9ab5a8c02887b7d453f0f52e8d1f01e6502342 Mon Sep 17 00:00:00 2001 From: brandonkelly Date: Tue, 22 May 2018 09:54:07 -0700 Subject: [PATCH] Don't wipe preloaded query results when setting the 'with' param Fixes #1576 --- CHANGELOG-v3.md | 1 + src/elements/db/ElementQuery.php | 12 ++++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/CHANGELOG-v3.md b/CHANGELOG-v3.md index 77d209a21e8..ac44181d9eb 100644 --- a/CHANGELOG-v3.md +++ b/CHANGELOG-v3.md @@ -11,6 +11,7 @@ - `craft\base\Plugin` now sets the default `$controllerNamespace` value to the plugin class’ namespace + `\controllers` or `\console\controllers`, depending on whether it’s a web or console request. - Improved the contrast of success and error notices in the Control Panel to meet WCAG AA requirements. ([#2885](https://github.com/craftcms/cms/issues/2885)) - `fieldValue` is now a protected field handle. ([#2893](https://github.com/craftcms/cms/issues/2893)) +- Craft will no longer discard any preloaded elements when setting the `with` param on an element query, fixing a bug where disabled Matrix blocks could show up in Live Preview if any nested fields were getting eager-loaded. ([#1576](https://github.com/craftcms/cms/issues/1576)) ### Fixed - Fixed a bug where the Plugin Store was listing featured plugins (e.g. “Recently Added”) in alphabetical order rather than the API-defined order. ([pixelandtonic/craftnet#83](https://github.com/pixelandtonic/craftnet/issues/83)) diff --git a/src/elements/db/ElementQuery.php b/src/elements/db/ElementQuery.php index 3781c4a20d5..ce4426b1266 100644 --- a/src/elements/db/ElementQuery.php +++ b/src/elements/db/ElementQuery.php @@ -20,6 +20,7 @@ use craft\errors\SiteNotFoundException; use craft\events\CancelableEvent; use craft\events\PopulateElementEvent; +use craft\helpers\ArrayHelper; use craft\helpers\Db; use craft\helpers\ElementHelper; use craft\helpers\StringHelper; @@ -1042,6 +1043,9 @@ public function all($db = null) { // Cached? if (($cachedResult = $this->getCachedResult()) !== null) { + if ($this->with) { + Craft::$app->getElements()->eagerLoadElements($this->elementType, $cachedResult, $this->with); + } return $cachedResult; } @@ -1114,7 +1118,6 @@ public function getCachedResult() // Make sure the criteria hasn't changed if ($this->_resultCriteria !== $this->getCriteria()) { $this->_result = null; - return null; } @@ -1142,7 +1145,12 @@ public function setCachedResult(array $elements) */ public function getCriteria(): array { - return $this->toArray($this->criteriaAttributes(), [], false); + $attributes = $this->criteriaAttributes(); + + // Ignore the 'with' param + ArrayHelper::removeValue($attributes, 'with'); + + return $this->toArray($attributes, [], false); } /**