craftcms / cms

Build bespoke content experiences with Craft.
https://craftcms.com
Other
3.22k stars 626 forks source link

[4.x]: Eager loading broken #12648

Closed vandres closed 1 year ago

vandres commented 1 year ago

What happened?

Description

I updated to Craft 4.3.8 and 4.3.8.1 afterwards. Since then eager loading stopped working, if the relation is empty or not set in the field layout.

We use Craft Commerce and have products, which can have allergy information. But not every product type has that information and the relation is optional.

{% do craft.app.elements.eagerLoadElements(
    className(product),
    [product],
    [
        'allergy',
    ]
) %}

Stack trace

2023-02-08 13:37:00 [web.INFO] [yii\db\Connection::open] Opening DB connection: mysql:host=db2;dbname=craft;port=3306 {"memory":11241416} 
2023-02-08 13:37:00 [web.INFO] [nystudio107\codeeditor\CodeEditor::bootstrap] CodeEditor module bootstrapped {"memory":15566160} 
2023-02-08 13:37:00 [web.INFO] [wbrowar\adminbar\AdminBar::init] Admin Bar plugin loaded {"memory":20292056} 
2023-02-08 13:37:00 [web.INFO] [bymayo\commercewidgets\CommerceWidgets::init] Commerce Widgets plugin loaded {"memory":23090016} 
2023-02-08 13:37:00 [web.INFO] [mmikkel\cpfieldinspect\CpFieldInspect::init] CP Field Inspect plugin loaded {"memory":23138168} 
2023-02-08 13:37:00 [web.INFO] [nystudio107\retour\Retour::init] Retour plugin loaded {"memory":24656744} 
2023-02-08 13:37:00 [web.INFO] [nystudio107\vite\Vite::init] Vite plugin loaded {"memory":24956544} 
2023-02-08 13:37:00 [web.INFO] [yii\web\Session::open] Session started {"memory":25188688} 
2023-02-08 13:37:00 [web.ERROR] [yii\base\ErrorException:2] yii\base\ErrorException: Undefined array key 0 in /var/www/vendor/craftcms/cms/src/fields/BaseRelationField.php:726
Stack trace:
#0 /var/www/vendor/craftcms/cms/src/web/ErrorHandler.php(79): yii\base\ErrorHandler->handleError(2, 'Undefined array...', '/var/www/vendor...', 726)
#1 /var/www/vendor/craftcms/cms/src/fields/BaseRelationField.php(726): craft\web\ErrorHandler->handleError(2, 'Undefined array...', '/var/www/vendor...', 726)
#2 /var/www/vendor/craftcms/cms/src/base/Element.php(1275): craft\fields\BaseRelationField->getEagerLoadingMap(Array)
#3 /var/www/vendor/craftcms/commerce/src/elements/Product.php(765): craft\base\Element::eagerLoadingMap(Array, 'allergy')
#4 /var/www/vendor/craftcms/cms/src/services/Elements.php(2553): craft\commerce\elements\Product::eagerLoadingMap(Array, 'allergy')
#5 /var/www/vendor/craftcms/cms/src/services/Elements.php(2517): craft\services\Elements->_eagerLoadElementsInternal('craft\\commerce\\...', Array, Array)
#6 /var/www/vendor/twig/twig/src/Extension/CoreExtension.php(1607): craft\services\Elements->eagerLoadElements('craft\\commerce\\...', Array, Array)
#7 /var/www/vendor/craftcms/cms/src/helpers/Template.php(110): twig_get_attribute(Object(craft\web\twig\Environment), Object(Twig\Source), Object(craft\services\Elements), 'eagerLoadElemen...', Array, 'method', false, false)
#8 /var/www/storage/runtime/compiled_templates/79/79a015e7b13b78aff1e0e0ba592329b9.php(46): craft\helpers\Template::attribute(Object(craft\web\twig\Environment), Object(Twig\Source), Object(craft\services\Elements), 'eagerLoadElemen...', Array, 'method')
#9 /var/www/vendor/twig/twig/src/Template.php(394): __TwigTemplate_521a010015d23881ed891f16d6cbad8f->doDisplay(Array, Array)
#10 /var/www/vendor/twig/twig/src/Template.php(367): Twig\Template->displayWithErrorHandling(Array, Array)
#11 /var/www/vendor/twig/twig/src/Template.php(379): Twig\Template->display(Array)
#12 /var/www/vendor/twig/twig/src/TemplateWrapper.php(40): Twig\Template->render(Array, Array)
#13 /var/www/vendor/twig/twig/src/Environment.php(277): Twig\TemplateWrapper->render(Array)
#14 /var/www/vendor/craftcms/cms/src/web/View.php(451): Twig\Environment->render('product/_entry....', Array)
#15 /var/www/vendor/craftcms/cms/src/web/View.php(504): craft\web\View->renderTemplate('product/_entry....', Array)
#16 /var/www/vendor/craftcms/cms/src/web/TemplateResponseFormatter.php(56): craft\web\View->renderPageTemplate('product/_entry....', Array, 'site')
#17 /var/www/vendor/yiisoft/yii2/web/Response.php(1098): craft\web\TemplateResponseFormatter->format(Object(craft\web\Response))
#18 /var/www/vendor/craftcms/cms/src/web/Response.php(286): yii\web\Response->prepare()
#19 /var/www/vendor/yiisoft/yii2/web/Response.php(339): craft\web\Response->prepare()
#20 /var/www/vendor/yiisoft/yii2/base/Application.php(390): yii\web\Response->send()
#21 /var/www/web/index.php(12): yii\base\Application->run()
#22 {main} {"memory":32033072,"exception":"[object] (yii\\base\\ErrorException(code: 2): Undefined array key 0 at /var/www/vendor/craftcms/cms/src/fields/BaseRelationField.php:726)"} 

Steps to reproduce

  1. Have a product type with a relation, which is optional
  2. Have a product type without the that relation
  3. Try to eager load and access them in the template

Expected behavior

Eager loading is working

Actual behavior

Eager loading is broken

Craft CMS version

4.3.8.1

PHP version

8.1

Operating system and version

MacOS, Debian Docker

Database type and version

MariaDB 10.5

Image driver and version

-

Installed plugins and versions

  "require": {
    "bymayo/commerce-widgets": "^3.0",
    "craftcms/cms": "^4.2.0.2",
    "craftcms/commerce": "^4.1.0",
    "craftcms/commerce-paypal-checkout": "dev-develop",
    "craftcms/redactor": "^3.0",
    "mmikkel/cp-field-inspect": "^1.4",
    "nystudio107/craft-retour": "^4.1",
    "nystudio107/craft-vite": "^4.0",
    "php-http/curl-client": "^2.2",
    "putyourlightson/craft-elements-panel": "^2.0",
    "putyourlightson/craft-sprig": "^2.1",
    "sebastianlenz/linkfield": "^2.1",
    "spatie/craft-ray": "^2.0",
    "spicyweb/craft-neo": "^3.2",
    "ttempleton/craft-nocache": "^3.0",
    "typesense/typesense-php": "^4.8",
    "vaersaagod/geomate": "^2.1.0",
    "verbb/expanded-singles": "^2.0",
    "verbb/field-manager": "^3.0",
    "verbb/tablemaker": "^4.0",
    "verbb/wishlist": "^2.0",
    "vlucas/phpdotenv": "^5.4.0",
    "wbrowar/adminbar": "^3.2"
  },
  "require-dev": {
    "deployer/deployer": "^7.0",
    "yiisoft/yii2-shell": "^2.0.3",
    "roave/security-advisories": "dev-master"
  },
vandres commented 1 year ago

I reverted back to 4.3.7.1 and it's working again

thupsi commented 1 year ago

I had a similar experience: https://github.com/craftcms/cms/issues/12651

With regular Craft though, not Craft Commerce.

simonleadbetter commented 1 year ago

I am also seeing an issue with 4.3.8.1 and eager loading matrix blocks. Getting Undefined array key 0 error. Downgraded to 4.3.7.1 and everything is working again.

The error is thrown if the entry does not contain the block which is being eager loaded.

gaelpleeroy commented 1 year ago

I just got this error too, I'll downgrade in the meanwhile

smcyr commented 1 year ago

I also got this error on my end Undefined array key 0 from the method getEagerLoadingMap().

samhibberd commented 1 year ago

I think w are also seeing this in Craft 3 - 3.7.65.1, downgrading to 3.7.65 fixes, for us was with logged in users only, but same error Undefined offset: 0:

Screenshot 2023-02-08 at 20 13 48

Not sure if you would prefer me to open a separate issue for Craft 3? I have also grabbed a snapshot of production logs if helpful.

yawaweb commented 1 year ago

I think w are also seeing this in Craft 3 - 3.7.65.1, downgrading to 3.7.65 fixes, for us was with logged in users only, but same error Undefined offset: 0:

Screenshot 2023-02-08 at 20 13 48

Not sure if you would prefer me to open a separate issue for Craft 3? I have also grabbed a snapshot of production logs if helpful.

I had the same error on 3.7.65.1

brandonkelly commented 1 year ago

Sorry about this. 3.7.65.2 and 4.3.8.2 are both out with a fix for this!