gpt4 book ai didi

magento - 创建后更新 Magento 产品属性的数据库字段类型

转载 作者:行者123 更新时间:2023-12-02 15:36:23 26 4
gpt4 key购买 nike

场景:您使用数据库迁移以编程方式创建了一个产品属性。几个月后,您希望将该属性从 VARCHAR 更改为 TEXT 字段类型。

创建后如何在保留数据的同时更改 EAV 属性的字段类型?

我的直觉是,Magento 的设置类不直接支持这一点,因为需要触摸无数的表、需要更新的记录以及需要从表复制到的内容表。

最佳答案

我不会假装这是最漂亮的解决方案,我怀疑它与数据库无关,但这是我的解决方案:

<?php
/** @var $this Mage_Eav_Model_Entity_Setup */

$this->startSetup();

$attribute_id = $this->getAttribute(
Mage_Catalog_Model_Product::ENTITY,
'your_attribute_code',
'attribute_id'
);;

if (!is_numeric($attribute_id)) {
Mage::throwException("Couldn't run migration: Unable to find attribute id");
}

/** @var Varien_Db_Adapter_Pdo_Mysql $connection */
$connection = $this->getConnection();

$connection->beginTransaction();

/**
* Copy the data from the VARCHAR table to the TEXT table.
*/
$connection->query("
INSERT INTO catalog_product_entity_text
(entity_type_id, attribute_id, store_id, entity_id, value)
SELECT
entity_type_id, attribute_id, store_id, entity_id, value
FROM catalog_product_entity_varchar
WHERE attribute_id = ?
",
array($attribute_id)
);

/**
* Update eav_attribute to use the text table instead of the varchar.
*/
$connection->query("UPDATE eav_attribute SET backend_type = 'text' WHERE attribute_id = ?", array($attribute_id));

/**
* Delete the attribute values from the VARCHAR table.
*/
$connection->query("DELETE FROM catalog_product_entity_varchar WHERE attribute_id = ?", array($attribute_id));
$connection->commit();

$this->endSetup();

关于magento - 创建后更新 Magento 产品属性的数据库字段类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16773434/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com