craft-snippets / Craft-quick-filters

Filter widgets for your Craft CMS control panel lists
Other
1 stars 1 forks source link

Calling the database during plugin initialization #6

Closed boudewijn-zicht closed 7 months ago

boudewijn-zicht commented 7 months ago

Using plugin version 2.2.1

We have many warnings in the logs, see the stack trace below. This occurs when the plugin is initialized by Craft, but the database isn't ready yet. Is this something you can help me with?

This happens every time we load the dashboard (and other pages). Why is a DB query performed by the craft-quick-filters plugin on the dashboard where the plugin shouldn't be doing anything?

2023-12-05 13:39:50 [web.WARNING] [craft\elements\db\ElementQuery::prepare] Element query executed before Craft is fully initialized.
Stack trace:
#0 /app/vendor/yiisoft/yii2/db/QueryBuilder.php(227): craft\elements\db\ElementQuery->prepare()
#1 /app/vendor/yiisoft/yii2/db/Query.php(157): yii\db\QueryBuilder->build()
#2 /app/vendor/yiisoft/yii2/db/Query.php(287): yii\db\Query->createCommand()
#3 /app/vendor/craftcms/cms/src/db/Query.php(275): yii\db\Query->one()
#4 /app/vendor/craftcms/cms/src/elements/db/ElementQuery.php(1604): craft\db\Query->one()
#5 /app/vendor/craftcms/cms/src/elements/User.php(545): craft\elements\db\ElementQuery->one()
#6 /app/vendor/yiisoft/yii2/web/User.php(698): craft\elements\User::findIdentity()
#7 /app/vendor/craftcms/cms/src/web/User.php(489): yii\web\User->renewAuthStatus()
#8 /app/vendor/yiisoft/yii2/web/User.php(199): craft\web\User->renewAuthStatus()
#9 /app/vendor/craftsnippets/craft-quick-filters/src/services/ElementFiltersService.php(313): yii\web\User->getIdentity()
#10 /app/vendor/craftsnippets/craft-quick-filters/src/ElementFilters.php(79): craftsnippets\elementfilters\services\ElementFiltersService->injectAssets()
#11 /app/vendor/yiisoft/yii2/base/BaseObject.php(109): craftsnippets\elementfilters\ElementFilters->init()
#12 /app/vendor/yiisoft/yii2/base/Module.php(161): yii\base\BaseObject->__construct()
#13 /app/vendor/craftcms/cms/src/base/Plugin.php(122): yii\base\Module->__construct()
#14 craft\base\Plugin->__construct()
#15 /app/vendor/yiisoft/yii2/di/Container.php(419): ReflectionClass->newInstanceArgs()
#16 /app/vendor/yiisoft/yii2/di/Container.php(170): yii\di\Container->build()
#17 /app/vendor/yiisoft/yii2/BaseYii.php(365): yii\di\Container->get()
#18 /app/vendor/craftcms/cms/src/Craft.php(59): yii\BaseYii::createObject()
#19 /app/vendor/craftcms/cms/src/services/Plugins.php(943): Craft::createObject()
#20 /app/vendor/craftcms/cms/src/services/Plugins.php(228): craft\services\Plugins->createPlugin()
#21 /app/vendor/craftcms/cms/src/base/ApplicationTrait.php(1616): craft\services\Plugins->loadPlugins()
#22 /app/vendor/craftcms/cms/src/web/Application.php(109): craft\web\Application->_postInit()
#23 /app/vendor/yiisoft/yii2/base/BaseObject.php(109): craft\web\Application->init()
#24 /app/vendor/yiisoft/yii2/base/Application.php(204): yii\base\BaseObject->__construct()
#25 yii\base\Application->__construct()
#26 /app/vendor/yiisoft/yii2/di/Container.php(419): ReflectionClass->newInstanceArgs()
#27 /app/vendor/yiisoft/yii2/di/Container.php(170): yii\di\Container->build()
#28 /app/vendor/yiisoft/yii2/BaseYii.php(365): yii\di\Container->get()
#29 /app/vendor/craftcms/cms/src/Craft.php(59): yii\BaseYii::createObject()
#30 /app/vendor/craftcms/cms/bootstrap/bootstrap.php(250): Craft::createObject()
#31 /app/vendor/craftcms/cms/bootstrap/web.php(40): require()
#32 /app/web/index.php(24): require() {"memory":38148912} 
piotrpog commented 7 months ago

@piotrpog Thanks for bringing this to my attention. Will look into this.

piotrpog commented 7 months ago

@boudewijn-zicht Fixed in the latest release, query is no longer being used before initialization. It was executed on dashboard due to checking for current user permissions before using asset bundle with plugin js. This happened on every control panel page. Right now asset bundle is used only when plugin template with filters is added to the elements list.

boudewijn-zicht commented 7 months ago

Thank you very much for the quick fix!

piotrpog commented 7 months ago

@boudewijn-zicht small update, turns out that plugin js needs to be injected on every page after all due to technical reasons (injecting js only when element list renders caused it to malfunction when viewing element list in element selection modal), so i released another update and plugin will be doing db query for checking current user after all. but it still does it after db initializes, so there still will be no warnings.