Magento2: loadAllAttributes fails for EAV entities

Created on 29 Jun 2016  路  11Comments  路  Source: magento/magento2

Preconditions

Magento 2.1.0

Steps to reproduce

  1. Install Magento 2.1.0
  2. Create a new EAV entity type 'my_eav_attr' with static and non-static attributes
  3. During run-time, create an instance of a collection that extends Magento\Eav\Model\Entity\Collection\AbstractCollection
  4. Call addAttributeToSelect('*') on collection

    Expected result

  5. A valid collection (as worked in 2.0.7)

    Actual result

main.CRITICAL: Exception: Serialization of 'Magento\Framework\View\Layout\Element' is not allowed in .../vendor/magento/module-eav/Model/Entity/AttributeCache.php:116
Stack trace:

0 .../vendor/magento/module-eav/Model/Entity/AttributeCache.php(116): serialize(Array)

1 .../vendor/magento/module-eav/Model/Entity/AttributeLoader.php(98): Magento\Eav\Model\Entity\AttributeCache->saveAttributes('my_eav_attr', Array)

2 .../vendor/magento/module-eav/Model/Entity/AbstractEntity.php(510): Magento\Eav\Model\Entity\AttributeLoader->loadAllAttributes(Object(MyModule\MyEAVType\Model\ResourceModel\Tile\Interceptor), NULL)

3 .../var/generation/MyModule/MyEAVType/Model/ResourceModel/Tile/Interceptor.php(193): Magento\Eav\Model\Entity\AbstractEntity->loadAllAttributes(NULL)

4 .../vendor/magento/module-eav/Model/Entity/Collection/AbstractCollection.php(484): MyModule\MyEAVType\Model\ResourceModel\MyEAVType\Interceptor->loadAllAttributes()

5 .../app/code/MyModule/MyEAVType/view/frontend/templates/myblock.phtml(6): Magento\Eav\Model\Entity\Collection\AbstractCollection->addAttributeToSelect('*')

Debugging in the attribute cache, it appears that serialisation of the static attributes is failing, although it's fine for the non-static attributes.

I've seen a suggestion that this is not an issue if I specify exactly which attributes I want, which is true, but the point of using EAV is that at compile time I will not always know the complete set of attributes I need.

Eav Fixed in 2.2.x Format is valid bug report

Most helpful comment

This also has something to do with the mode in which Magento is running.
I'm having the same issue when running in developer mode, but in production mode this is not an issue

All 11 comments

As a temporary measure I've had to exclude my EAV entity from attribute caching via di.xml:

  <type name="Magento\Eav\Model\Entity\AttributeCache">
        <arguments>
            <argument name="unsupportedTypes" xsi:type="array">
                <item name="0" xsi:type="string">my_eav_attr</item>
            </argument>
        </arguments>
    </type>

I experience the same issue after upgrading to M2.1

edit:
My temp fix is to just disable the EAV caching type
I'm running Community Edition

I'm running Magento EE 2.1.0 and I have the same issue since the upgrade with addAttributeToSelect('*') on custom entity collections.

This also has something to do with the mode in which Magento is running.
I'm having the same issue when running in developer mode, but in production mode this is not an issue

Hi,

We have the same problem after upgrading to magento CE 2.1.1

Same issue for me. Magento CE 2.1.1 and Magento EE 2.1.1

@maderlock I managed to get it working on custom entity attributes by reporting this line : https://github.com/magento/magento2/blob/develop/app/code/Magento/Catalog/Model/ResourceModel/Eav/Attribute.php#L827

to my attributes.

I think the "raw" EAV attributes are not usable "as is", because when looking at catalog eav attributes, they add a lot of code to get them working. There is this one regarding the __sleep() method but also custom methods to have a working scoping for attribute values.

Regards

I have the same issue after upgrading to CE 2.1.1. @jessetaverne temporary fix worked for me, thanks.

Yes I also fixed this by creating my own attribute model and adding the unsetData at __sleep():

<?php

namespace Example\Module\Model\Entity;

/**
 * Class Attribute
 * @package Example\Module\Model\Entity
 */
class Attribute extends \Magento\Eav\Model\Entity\Attribute
{
    /**
     * This fixes https://github.com/magento/magento2/issues/5339
     *
     * @inheritdoc
     */
    public function __sleep()
    {
        $this->unsetData('entity_type');
        return parent::__sleep();
    }
}

And after that putting this model in the eav_entity_type table (attribute model = "Example\Module\Model\Entity\Attribute" with my custom entity. This solves the issue like @romainruaud said.

I'm having this same problem in CE 2.1.7 when I extend \Magento\Catalog\Model\ResourceModel\Eav\Attribute and add an extra dependency to the constructor. The __sleep()-fix doesn't work, since it's parent class is already doing the same.

Buggy EAV Attribute Cache mechanism was removed on Magento 2.2.0 branch: https://github.com/magento/magento2/commit/3ae7c1909daad96d5a0ad6ab7048455448ba7480

This is not something that could be backported due to BC constraints, as a workaround you can try just to disable this cache type.

Was this page helpful?
0 / 5 - 0 ratings