Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't create an element query right away from ScoutIndex #300

Merged
merged 2 commits into from
May 26, 2024

Conversation

brandonkelly
Copy link
Contributor

@brandonkelly brandonkelly commented May 23, 2024

ScoutIndex::__construct() was creating an element query, which is problematic because ScoutIndex objects are meant to be created from the plugin’s config file, which will get executed before Craft is fully initialized. (Even the Scout 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.

@janhenckens janhenckens self-assigned this May 26, 2024
@janhenckens janhenckens merged commit 1feca10 into studioespresso:develop May 26, 2024
0 of 4 checks passed
@janhenckens
Copy link
Member

Thanks @brandonkelly, released in 4.1.0, I'll make sure it is included in the next release for Craft 5 as well.

janhenckens added a commit that referenced this pull request May 26, 2024
@brandonkelly
Copy link
Contributor Author

Thanks @janhenckens!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants