Magento2: Magento 2.1 product multiselect EAV not loading saved data

Created on 4 Jul 2016  路  12Comments  路  Source: magento/magento2

Having a similar issue to this https://github.com/magento/magento2/issues/4822

Steps to reproduce

  1. Install Magento from develop branch.
  2. add multiselect as follows:
$this->addMultiSelectFieldWithSource($eavSetup, 'special_requirements', 'Special requirements', false, 4,'Amrita\Catalog\Model\Product\Attribute\Source\SpecialRequirements');   

 private function addMultiSelectFieldWithSource($eavSetup, $fieldName, $fieldTitle, $required, $position, $source) {
    $eavSetup->addAttribute(
        Product::ENTITY,
        $fieldName,
        [
            'type' => 'varchar',
            'label' => $fieldTitle,
            'input' => 'multiselect',
            'required' => $required,
            'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
            'source' => $source,
            'global' => Attribute::SCOPE_GLOBAL,
            'visible' => true,
            'user_defined' => true,
            'system' => 0,
            'searchable' => true,
            'visible_in_advanced_search' => true,
            'filterable' => true,
            'filterable_in_search' => true,
            'position' => $position,
            'comparable' => false,
            'visible_on_front' => false,
            'unique' => false,
            'group' => 'General',
            'is_used_in_grid' => true,
            'is_visible_in_grid' => false,
            'is_filterable_in_grid' => true,
        ]
    );
  1. Select and save an option against the attribute

    Expected result

  2. Multiselect field retrieves selected value.

    Actual result

  3. selected value is saved the first time in the db table catalog_product_entity_varchar, and the attribute is added to the eav_attribute table

  4. but when i load the edit product view the data does not load, i.e. the selected options are not highlighted against the attribute.

image

The attributes installed with the sample data work correctly, but i cannot understand how the magento installation differs to my attempted installation.

Eav Confirmed Format is not valid Ready for Work Reproduced on 2.1.x Reproduced on 2.2.x Reproduced on 2.3.x bug report

Most helpful comment

Hello You can solve this issue by adding string cast to the option value of your source model
like this
public function getAllOptions()
{
$collection = $this->getCollection();
$_options = [];
foreach ($collection as $item) {
$_options[] = [
'label' => $item->getName(),
'value' => (string)$item->getId()
];
}
return $_options;
}
it is worked for me

All 12 comments

Is there any update on this, i'm aware this was an issue in the previous version but this hasn't been resolved for me through upgrading to 2.1?

It looks like this can be fixed by having string values instead of integers in the source. So ['value' => 1, ...] becomes ['value' => '1'].

I would say that this is still a bug, though.

This bug is not constrained to products only. Customer cannot save a multiselect. I can only assume it affects other entities.

Hello You can solve this issue by adding string cast to the option value of your source model
like this
public function getAllOptions()
{
$collection = $this->getCollection();
$_options = [];
foreach ($collection as $item) {
$_options[] = [
'label' => $item->getName(),
'value' => (string)$item->getId()
];
}
return $_options;
}
it is worked for me

Solution suggested by @zexperto solved my problem. And it's kinda explain the issue.
If you explode a string separated by comma it will results in an array of strings not integers.

[Update: 15/03/2018] Solution won't work on static data source with string values.
Example:


    public function getAllOptions()
    {
        if (!$this->_options) {
            $this->_options = [
                ['value' => 'one', 'label' => __('Label 1')],
                ['value' => 'two', 'label' => __('Label 2')],
                ['value' => 'three', 'label' => __('Label 3')],
                ['value' => 'other', 'label' => __('Other')],
            ];
        }

        return $this->_options;
    }

Solution suggested by @zexperto dont work for me, magento version 2.2

@sma09sjb, thank you for your report.
We've created internal ticket(s) MAGETWO-84479 to track progress on the issue.

Just like @kovalevandrew i'm still seeing this issue on 2.2.2

Either defining the backend type as varchar, or keeping it as int and explicitly casting as ints work for me. In truth the, the ArrayBackend model should really double check what you're saving as and cast it as that.

Thanks to everyone in this thread for pointing me in the direction of a fix.

@magento-engcom-team Any update or bug fix on this ticket?

@sma09sjb @tuyennn

Fix for this issue is going to be available with the upcoming 2.2.6 release.
Meanwhile, you may use the following commits as the reference: https://github.com/search?q=MAGETWO-90576&type=Commits

Thank you

Hello You can solve this issue by adding string cast to the option value of your source model
like this
public function getAllOptions()
{
$collection = $this->getCollection();
$_options = [];
foreach ($collection as $item) {
$_options[] = [
'label' => $item->getName(),
'value' => (string)$item->getId()
];
}
return $_options;
}
it is worked for me

How do i do this? (sorry, programming n00b) :)

Was this page helpful?
0 / 5 - 0 ratings