[4.2.5]: relatedTo with multiply Categories Error: Serialization of 'Closure' is not allowed
What happened?
Description
After updating to Craft 4 this query cause an error at query
{% paginate craft.entries.section(['blog']).relatedTo(block.category).with(['ovImage']).limit(block.limit) as pageInfo, pageEntries %}
block.category is a category field and can include multiple categories. If just one category is selected no error appears but also no data, with more categories selected: Serialization of 'Closure' is not allowed
Craft CMS version
4.2.5
PHP version
No response
Operating system and version
No response
Database type and version
No response
Image driver and version
No response
Installed plugins and versions
I'm having trouble replicating this one. Are there any errors in the storage/logs files that might provide more clues?
there are tons of errors, which file do you want? ;) Also the debug bar does not work for me.
It shows: PHP Warning – yii\base\ErrorException Trying to access array offset on value of type null
if you want to, i can send you all files via email
That's the output of the page
Exception: Serialization of 'Closure' is not allowed in /var/www/craft/vendor/yiisoft/yii2/db/Query.php:1399
Stack trace:
#0 /var/www/craft/vendor/yiisoft/yii2/db/Query.php(1399): serialize(Object(craft\elements\db\CategoryQuery))
#1 [internal function]: yii\db\Query->__toString()
#2 /var/www/craft/vendor/craftcms/cms/src/elements/db/ElementRelationParamParser.php(73): in_array(Object(craft\elements\db\CategoryQuery), Array)
#3 /var/www/craft/vendor/craftcms/cms/src/elements/db/ElementRelationParamParser.php(185): craft\elements\db\ElementRelationParamParser::normalizeRelatedToParam(Array)
#4 /var/www/craft/vendor/craftcms/cms/src/elements/db/ElementQuery.php(2237): craft\elements\db\ElementRelationParamParser->parse(Object(craft\elements\db\CategoryQuery))
#5 /var/www/craft/vendor/craftcms/cms/src/elements/db/ElementQuery.php(1376): craft\elements\db\ElementQuery->_applyRelatedToParam()
#6 /var/www/craft/vendor/yiisoft/yii2/db/QueryBuilder.php(227): craft\elements\db\ElementQuery->prepare(Object(craft\db\mysql\QueryBuilder))
#7 /var/www/craft/vendor/yiisoft/yii2/db/Query.php(157): yii\db\QueryBuilder->build(Object(craft\elements\db\EntryQuery))
#8 /var/www/craft/vendor/yiisoft/yii2/db/Query.php(249): yii\db\Query->createCommand(Object(craft\db\Connection))
#9 /var/www/craft/vendor/craftcms/cms/src/db/Query.php(247): yii\db\Query->all(NULL)
#10 /var/www/craft/vendor/craftcms/cms/src/elements/db/ElementQuery.php(1471): craft\db\Query->all(NULL)
#11 /var/www/craft/vendor/craftcms/cms/src/db/Paginator.php(220): craft\elements\db\ElementQuery->all(NULL)
#12 /var/www/craft/vendor/craftcms/cms/src/web/twig/variables/Paginate.php(33): craft\db\Paginator->getPageResults()
#13 /var/www/craft/vendor/craftcms/cms/src/helpers/Template.php(139): craft\web\twig\variables\Paginate::create(Object(craft\db\Paginator))
#14 /var/www/craft/storage/runtime/compiled_templates/46/4654b27a0d34b054638a032976d5b3698647800faea2b381f4e97f49ca769512.php(69): craft\helpers\Template::paginateQuery(Object(craft\elements\db\EntryQuery))
#15 /var/www/craft/vendor/twig/twig/src/Template.php(171): __TwigTemplate_6ede277d3418406679253da9cd52abb75f522204c91164af393085990e458a53->block_moduleContent(Array, Array)
#16 /var/www/craft/storage/runtime/compiled_templates/85/8583210cd262d0a75f6272f80ccd535d62963ca1dff6c454b237385e5eb692a1.php(49): Twig\Template->displayBlock('moduleContent', Array, Array)
#17 /var/www/craft/vendor/twig/twig/src/Template.php(394): __TwigTemplate_05383b984effc23a0b1d74e45bec014a25ab1547dfb61563355ae8fa8cf1d640->doDisplay(Array, Array)
#18 /var/www/craft/vendor/twig/twig/src/Template.php(367): Twig\Template->displayWithErrorHandling(Array, Array)
#19 /var/www/craft/storage/runtime/compiled_templates/46/4654b27a0d34b054638a032976d5b3698647800faea2b381f4e97f49ca769512.php(44): Twig\Template->display(Array, Array)
#20 /var/www/craft/vendor/twig/twig/src/Template.php(394): __TwigTemplate_6ede277d3418406679253da9cd52abb75f522204c91164af393085990e458a53->doDisplay(Array, Array)
#21 /var/www/craft/vendor/twig/twig/src/Template.php(367): Twig\Template->displayWithErrorHandling(Array, Array)
#22 /var/www/craft/storage/runtime/compiled_templates/09/09452eb6ac5a3c2af424601212142c21111b9ca356e3fcfd99f02a61ac1453bf.php(112): Twig\Template->display(Array)
#23 /var/www/craft/vendor/twig/twig/src/Template.php(394): __TwigTemplate_c41c38d53a48d86de7b9ed469959c9ea211bec4435d9a5d65428aa3fb44c306d->doDisplay(Array, Array)
#24 /var/www/craft/vendor/twig/twig/src/Template.php(367): Twig\Template->displayWithErrorHandling(Array, Array)
#25 /var/www/craft/storage/runtime/compiled_templates/bb/bbef76ecf0fe39c5ba41a0356ae75e3bf3c021214915a7037411c0d7ed5dbb88.php(85): Twig\Template->display(Array)
#26 /var/www/craft/vendor/twig/twig/src/Template.php(394): __TwigTemplate_914c0a5db763724229cbff90abcbcd0de384d88198c763624515b727b2f498df->doDisplay(Array, Array)
#27 /var/www/craft/vendor/twig/twig/src/Template.php(367): Twig\Template->displayWithErrorHandling(Array, Array)
#28 /var/www/craft/storage/runtime/compiled_templates/3c/3c7e2f87f2ae22292950e7bc53ba58cce8f645242aa7d51b5a026ca57608e350.php(127): Twig\Template->display(Array)
#29 /var/www/craft/vendor/twig/twig/src/Template.php(171): __TwigTemplate_1c0dcc7295ea72c5e24f4eb711ff01668ee1b9359292adcb2201e7f7f5633316->block_content(Array, Array)
#30 /var/www/craft/storage/runtime/compiled_templates/4a/4a48c8cfa9c78124de4afc18ec1739be5236153a72d584f19ce809ee355547a4.php(171): Twig\Template->displayBlock('content', Array, Array)
#31 /var/www/craft/vendor/twig/twig/src/Template.php(394): __TwigTemplate_b44856ee36783e20ced7f5ae5d660e0b2fc2877fb084d915438e5f9291d6e154->doDisplay(Array, Array)
#32 /var/www/craft/vendor/twig/twig/src/Template.php(367): Twig\Template->displayWithErrorHandling(Array, Array)
#33 /var/www/craft/storage/runtime/compiled_templates/3c/3c7e2f87f2ae22292950e7bc53ba58cce8f645242aa7d51b5a026ca57608e350.php(52): Twig\Template->display(Array, Array)
#34 /var/www/craft/vendor/twig/twig/src/Template.php(394): __TwigTemplate_1c0dcc7295ea72c5e24f4eb711ff01668ee1b9359292adcb2201e7f7f5633316->doDisplay(Array, Array)
#35 /var/www/craft/vendor/twig/twig/src/Template.php(367): Twig\Template->displayWithErrorHandling(Array, Array)
#36 /var/www/craft/vendor/twig/twig/src/Template.php(379): Twig\Template->display(Array)
#37 /var/www/craft/vendor/twig/twig/src/TemplateWrapper.php(40): Twig\Template->render(Array, Array)
#38 /var/www/craft/vendor/twig/twig/src/Environment.php(277): Twig\TemplateWrapper->render(Array)
#39 /var/www/craft/vendor/craftcms/cms/src/web/View.php(428): Twig\Environment->render('pages/_entry', Array)
#40 /var/www/craft/vendor/craftcms/cms/src/web/View.php(481): craft\web\View->renderTemplate('pages/_entry', Array)
#41 /var/www/craft/vendor/craftcms/cms/src/web/TemplateResponseFormatter.php(56): craft\web\View->renderPageTemplate('pages/_entry', Array, 'site')
#42 /var/www/craft/vendor/yiisoft/yii2/web/Response.php(1095): craft\web\TemplateResponseFormatter->format(Object(craft\web\Response))
#43 /var/www/craft/vendor/craftcms/cms/src/web/Response.php(286): yii\web\Response->prepare()
#44 /var/www/craft/vendor/yiisoft/yii2/web/Response.php(339): craft\web\Response->prepare()
#45 /var/www/craft/vendor/yiisoft/yii2/base/Application.php(390): yii\web\Response->send()
#46 /var/www/craft/web/index.php(21): yii\base\Application->run()
#47 {main}```
Thanks! Unfortunately, I'm still not able to replicate on my end. Can you clear out your storage/runtime/compiled_templates folder to see if that helps?
If not, can you send a database backup and your composer files over to [email protected]?
Does not work, i will send you the files
Just my two cents: I can confirm this is indeed a Craft bug.. in the end it all comes down due to heavy casting on multiple places. A trainee had this a few weeks ago but after analyzing the logs I just think it's just not worth to pass Queries as a related to parameter.
In general just as a quick "fix" to avoid this bug (plus gain performance) I would suggest to execute your query and pass the result as related to instead of the query itself.
As of Craft: it's easily fixable if you execute the query at the beginning instead of casting it all the time. Btw it only happens when the Query is prepared a second time..
Ok:
$relatedTo = Entry::find()->section('someSection');
$query = Entry::find()->relatedTo($relatedTo);
$query->ids()
Not okay:
$relatedTo = Entry::find()->section('someSection');
$query = Entry::find()->relatedTo($relatedTo);
$query->ids();
$query->ids(); // or all or getRawSql or something like that
Hi @Anubarak indeed it worked like this:
{% set ids = block.category.ids %}
{% set query = craft.entries
.section(['blog'])
.relatedTo(ids)
.with(['ovImage'])
.limit(block.limit) %}
thanks a lot!
We tracked this down to a bug that could have potentially affected Craft 3 as well, so we’ve now fixed this for the next Craft 3 and 4 releases. (#12055)
Craft 3.7.56 and 4.2.6 are out with the fix for this.