更新数据库字段

Update field of database

我有这个代码来更新属性的最大长度

class UpgradeData implements UpgradeDataInterface
{
    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();
        if ($context->getVersion()
            && version_compare($context->getVersion(), '1.0.2') < 0
        ) {
            $table = $setup->getTable('eav_attribute');
            $setup->getConnection()
                ->update($table, ['frontend_class' => 'validate-length maximum-length-70'], 'attribute_id = 73');
        }
        $setup->endSetup();
    }
}

它工作正常,但我想输入 attribute_code= name 而不是 attribute_id = 73,但它不起作用。

试试这个

class UpgradeData implements UpgradeDataInterface
{
    /**
     * @var \Magento\Eav\Setup\EavSetupFactory
     */
    private $eavSetupFactory;
    
    public function __construct(\Magento\Eav\Setup\EavSetupFactory $eavSetupFactory)
    {
         $this->eavSetupFactory = $eavSetupFactory;
    }
    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();
        if ($context->getVersion()
            && version_compare($context->getVersion(), '1.0.2') < 0
        ) {
            $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
            $this->eavSetup->updateAttribute(
                'catalog_product',
                'ATTRIBUTE_CODE_GOES_HERE',
                'frontend_class',
                'validate-length maximum-length-70'
            );
        }
        $setup->endSetup();
    }
}

注意错别字。我没有检查代码。