0

I have a product attribute that have been added directly in the database threw a migration process.

I want to update that attribute to give him a source.

But this seems not to be working.

$attribute = $this->productAttributeRepositoryInterface->get('my_attribute_code');
$attributeId = $attribute->getAttributeId();
$this->eavSetupFactory->updateAttribute(
    Product::ENTITY,
    $attributeId,
    [
        'source' => 'Cpy\Module\Model\ResourceModel\MyAttribute\Options',
    ]
);

I have no error but the backend model in database seems to stay null.

Also tried :

        try{
            $attribute = $this->productAttributeRepositoryInterface->get('encombrement');
            $attributeId = $attribute->getAttributeId();
            $this->eavSetupFactory->updateAttribute(
                4,
                $attributeId,
                'source',
                'Cpy\Module\Model\ResourceModel\MyAttribute\Options'
            );
        }catch(Exception $e){
            var_dump($e->getMessage());
        }

Full method

public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
    $setup->startSetup();
    if (version_compare($context->getVersion(), '0.0.2') < 0) {
       // ....
    }
    if (version_compare($context->getVersion(), '0.0.3') < 0) {
        try{
            $attribute = $this->productAttributeRepositoryInterface->get('encombrement');
            $attributeId = $attribute->getAttributeId();
            $this->eavSetupFactory->updateAttribute(
                4,
                $attributeId,
                'source',
                'Cpy\Module\Model\ResourceModel\MyAttribute\Options'
            );
        }catch(Exception $e){
            var_dump($e->getMessage());
        }

    }
    $setup->endSetup();
}

2 Answers 2

1
<?php

namespace VVendor\Extension\Setup; 

use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Eav\Setup\EavSetup; 

class UpgradeData implements UpgradeDataInterface 
{
    public function __construct(
        EavSetup $eavSetupFactory
        ) 
    { 
        $this->eavSetupFactory = $eavSetupFactory; 
    } 

    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { 
        $setup->startSetup();
        $this->eavSetupFactory->updateAttribute(4,32,'source','Cpy\Module\Model\ResourceModel\MyAttribute\Options',null); 
        $setup->endSetup(); 
    } 

}

NOTE:

4 = Entity Type Id of Attribute.
32 = Attribute Id.
9
  • Seems not to work neither, i still see the model value empty in database after updating this way :/ In the product form the select is still empty too Commented Feb 28, 2022 at 13:11
  • Confirm you replace proper data into above code and then run caching, indexing, upgrade and deploy commands. Commented Feb 28, 2022 at 13:13
  • I'm in dev mode so no deploy. I've run the setup upgrade and flush cache after it thats what i still have in the database : 123,4,encombrement,,,int,,,select,Encombrement,,,0,1,"",0, Commented Feb 28, 2022 at 13:18
  • updated code in the question Commented Feb 28, 2022 at 13:20
  • Where is $setup->endSetup(); this line into your code? Commented Feb 28, 2022 at 13:24
0

etc/module.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Vendor_Extension" setup_version="1.0.0"/>
</config>

Setup/UpgradeData.php

<?php

namespace Vendor\Extension\Setup;

use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class UpgradeData implements UpgradeDataInterface
{

    public function __construct(
        \Magento\Eav\Setup\EavSetupFactory $eavSetupFactory
    ) {
        $this->_eavSetupFactory = $eavSetupFactory;
    }

    /**
     * {@inheritdoc}
     */
    public function upgrade(
        ModuleDataSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        if (version_compare($context->getVersion(), "1.0.0", "<")) {
            $this->updateAttribute($setup);
        }
    }

    public function updateAttribute($setup)
    {
        $eavSetup = $this->_eavSetupFactory->create();
        $eavSetup->updateAttribute(4, 'encombrement', 'source_model', 'Cpy\Module\Model\ResourceModel\MyAttribute\Options');
    }
}

Note:

  • 4 or 'catalog_product' for Entity Type
  • better to use attribute_code instead of attribute_id

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.