You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
The text was updated successfully, but these errors were encountered:
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 theinit
method, e.g.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:
Environmental Info
The text was updated successfully, but these errors were encountered: