Don't create an element query right away from ScoutIndex #300
+5
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
ScoutIndex::__construct()
was creating an element query, which is problematic becauseScoutIndex
objects are meant to be created from the plugin’s config file, which will get executed before Craft is fully initialized. (Even theScout
class won’t have been loaded yet at that point, let alone other plugins.) If another plugin hasn’t been loaded yet, it won’t have had a chance to register event listeners for the element query yet, etc.In one case we are seeing this, combined with some other random plugin also doing too much before the app is fully initialized, end up causing a
Class "craft\behaviors\CustomFieldBehavior" does not exist
error whenever the class needs to be recreated.This fix defers the element query creation until the first time it’s actually needed, which in theory will be a bit further along in the request.
Plus, it avoids needlessly creating an element query if
ScoutIndex::criteria()
ends up getting called, in which case$this->_criteria
was getting discarded in favor of a new element query instance, anyway.