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

[2.x] Craft 4: Element query executed before Craft is fully initialized. #231

Closed
d--j opened this issue Jul 5, 2022 · 1 comment · Fixed by #232
Closed

[2.x] Craft 4: Element query executed before Craft is fully initialized. #231

d--j opened this issue Jul 5, 2022 · 1 comment · Fixed by #232

Comments

@d--j
Copy link
Contributor

d--j commented Jul 5, 2022

lenz\linkfield\listeners\ElementListenerState->flush gets called in a Plugins::EVENT_AFTER_LOAD_PLUGINS event handler. That is before the application is marked as fully initialized and thus every ElementQuery will output a warning, that the application is not fully initialized:
https://github.com/craftcms/cms/blob/3caf4c317bb581bc3e063b026eb339a56625a945/src/elements/db/ElementQuery.php#L1224-L1226

Use the WebApplication::EVENT_INIT event instead (besides its name, it also gets fired in Console request):
https://github.com/craftcms/cms/blob/3caf4c317bb581bc3e063b026eb339a56625a945/src/base/ApplicationTrait.php#L1476-L1484

Here's a stack trace of one of these warnings (converted into an exception):

2022-07-05 12:51:57 [console.ERROR] [application] Exception 'LogicException' with message 'Element query executed before Craft is fully initialized.' 

in /app/app/vendor/craftcms/cms/src/elements/db/ElementQuery.php:1226

Stack trace:
#0 /app/app/vendor/yiisoft/yii2/db/QueryBuilder.php(227): craft\elements\db\ElementQuery->prepare(Object(craft\db\mysql\QueryBuilder))
#1 /app/app/vendor/yiisoft/yii2/db/Query.php(157): yii\db\QueryBuilder->build(Object(craft\elements\db\EntryQuery))
#2 /app/app/vendor/yiisoft/yii2/db/Query.php(287): yii\db\Query->createCommand(Object(craft\db\Connection))
#3 /app/app/vendor/craftcms/cms/src/db/Query.php(180): yii\db\Query->one(NULL)
#4 /app/app/vendor/craftcms/cms/src/elements/db/ElementQuery.php(1471): craft\db\Query->one(NULL)
#5 /app/app/vendor/sebastianlenz/linkfield/src/listeners/ElementListenerState.php(233): craft\elements\db\ElementQuery->one()
#6 /app/app/vendor/sebastianlenz/linkfield/src/listeners/ElementListenerState.php(143): lenz\linkfield\listeners\ElementListenerState->loadNextEntryChangeDate()
#7 /app/app/vendor/sebastianlenz/linkfield/src/listeners/ElementListener.php(49): lenz\linkfield\listeners\ElementListenerState->flush()
#8 /app/app/vendor/sebastianlenz/linkfield/src/Plugin.php(90): lenz\linkfield\listeners\ElementListener->processStatusChanges()
#9 [internal function]: lenz\linkfield\Plugin->onAfterLoadPlugins(Object(yii\base\Event))
#10 /app/app/vendor/yiisoft/yii2/base/Event.php(312): call_user_func(Array, Object(yii\base\Event))
#11 /app/app/vendor/yiisoft/yii2/base/Component.php(642): yii\base\Event::trigger('craft\\services\\...', 'afterLoadPlugin...', Object(yii\base\Event))
#12 /app/app/vendor/craftcms/cms/src/services/Plugins.php(275): yii\base\Component->trigger('afterLoadPlugin...')
#13 /app/app/vendor/craftcms/cms/src/base/ApplicationTrait.php(1477): craft\services\Plugins->loadPlugins()
#14 /app/app/vendor/craftcms/cms/src/console/Application.php(52): craft\console\Application->_postInit()
#15 /app/app/vendor/yiisoft/yii2/base/BaseObject.php(109): craft\console\Application->init()
#16 /app/app/vendor/yiisoft/yii2/base/Application.php(204): yii\base\BaseObject->__construct(Array)
#17 /app/app/vendor/yiisoft/yii2/console/Application.php(89): yii\base\Application->__construct(Array)
#18 [internal function]: yii\console\Application->__construct(Array)
#19 /app/app/vendor/yiisoft/yii2/di/Container.php(419): ReflectionClass->newInstanceArgs(Array)
#20 /app/app/vendor/yiisoft/yii2/di/Container.php(170): yii\di\Container->build('craft\\console\\A...', Array, Array)
#21 /app/app/vendor/yiisoft/yii2/BaseYii.php(365): yii\di\Container->get('craft\\console\\A...', Array, Array)
#22 /app/app/vendor/craftcms/cms/src/Craft.php(53): yii\BaseYii::createObject(Array, Array)
#23 /app/app/vendor/craftcms/cms/bootstrap/bootstrap.php(239): Craft::createObject(Array)
#24 /app/app/vendor/craftcms/cms/bootstrap/console.php(40): require('/app/app/vendor...')
#25 /app/app/craft(12): require('/app/app/vendor...')
#26 {main} {"memory":20457680} 
@maxstrebel
Copy link

@sebastian-lenz will there be a release with this fix? Our logs are full of this error.

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 a pull request may close this issue.

2 participants