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

Bulk Edit plugin triggering 'Element query executed before Craft is fully initialized.' errors #45

Closed
martyspain opened this issue Sep 28, 2022 · 0 comments · Fixed by #48
Assignees
Labels
bug Something isn't working

Comments

@martyspain
Copy link

martyspain commented Sep 28, 2022

Describe the bug
As of Craft 4, element queries that are executed before Craft is fully initialised will not work and will result in a warning being logged that says 'Element query executed before Craft is fully initialized.'

It seems that this is usually caused by element queries being executed from a plugin's init method (see discussion here), and from looking at the Bulk Edit source code, there's a lot of queries to check user permissions in the init method, e.g.

if (Craft::$app->user->checkPermission(self::PERMISSION_BULKEDIT_ENTRIES)) {
...
}

This results in a call to getIdentity() which executes a user query to get the current user.

From the guidance in the thread linked above, those queries should be deferred until Craft is fully initialised by listening for the Application::EVENT_INIT event. At the moment, my logs are filled with line after line of 'Element query executed before Craft is fully initialized.', making it hard to debug other issues.

Screenshots
Stack trace:

2022-09-28 10:07:07 [web.WARNING] [craft\elements\db\ElementQuery::prepare] Element query executed before Craft is fully initialized. {"trace":[{"file":"/var/www/html/vendor/craftcms/cms/src/elements/db/ElementQuery.php","line":1241,"function":"warning","class":"yii\\BaseYii","type":"::"},{"file":"/var/www/html/vendor/craftcms/cms/src/db/Query.php","line":274,"function":"one","class":"yii\\db\\Query","type":"->"},{"file":"/var/www/html/vendor/craftcms/cms/src/elements/db/ElementQuery.php","line":1485,"function":"one","class":"craft\\db\\Query","type":"->"},{"file":"/var/www/html/vendor/craftcms/cms/src/elements/User.php","line":532,"function":"one","class":"craft\\elements\\db\\ElementQuery","type":"->"},{"file":"/var/www/html/vendor/craftcms/cms/src/web/User.php","line":500,"function":"renewAuthStatus","class":"yii\\web\\User","type":"->"},{"file":"/var/www/html/vendor/craftcms/cms/src/web/User.php","line":270,"function":"getIdentity","class":"yii\\web\\User","type":"->"},{"file":"/var/www/html/vendor/venveo/craft-bulkedit/src/Plugin.php","line":83,"function":"checkPermission","class":"craft\\web\\User","type":"->"},{"file":"/var/www/html/vendor/craftcms/cms/src/base/Plugin.php","line":122,"function":"__construct","class":"yii\\base\\Module","type":"->"},{"file":"/var/www/html/vendor/craftcms/cms/src/Craft.php","line":53,"function":"createObject","class":"yii\\BaseYii","type":"::"},{"file":"/var/www/html/vendor/craftcms/cms/src/services/Plugins.php","line":949,"function":"createObject","class":"Craft","type":"::"},{"file":"/var/www/html/vendor/craftcms/cms/src/services/Plugins.php","line":228,"function":"createPlugin","class":"craft\\services\\Plugins","type":"->"},{"file":"/var/www/html/vendor/craftcms/cms/src/base/ApplicationTrait.php","line":1477,"function":"loadPlugins","class":"craft\\services\\Plugins","type":"->"},{"file":"/var/www/html/vendor/craftcms/cms/src/web/Application.php","line":105,"function":"_postInit","class":"craft\\web\\Application","type":"->"},{"file":"/var/www/html/vendor/craftcms/cms/src/Craft.php","line":53,"function":"createObject","class":"yii\\BaseYii","type":"::"},{"file":"/var/www/html/vendor/craftcms/cms/bootstrap/bootstrap.php","line":239,"function":"createObject","class":"Craft","type":"::"},{"file":"/var/www/html/vendor/craftcms/cms/bootstrap/web.php","line":40,"function":"require"}],"memory":10123624} 

Environmental Info

  • Craft CMS Version: 4.2.5.1
  • Multi-site Mode (Yes/No): No
  • Plugin Version: 4.0.0-RC1
  • PHP Version: 8.0
  • Database Driver (MySQL/Postgres): MySQL
@martyspain martyspain added the bug Something isn't working label Sep 28, 2022
@Mosnar Mosnar self-assigned this Nov 24, 2022
@Mosnar Mosnar mentioned this issue Nov 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants