方案:您已經使用數據庫遷移以編程方式創建了產品屬性。幾個月後,您希望將該屬性從VARCHAR
更改爲TEXT
字段類型。創建後更新Magento產品屬性的數據庫字段類型
如何在保存數據的同時在創建後更改EAV屬性的字段類型?
我的直覺是,這不是通過Magento的安裝類直接支持的,因爲需要觸摸大量的表,需要更新的記錄以及需要從表中複製的內容到桌子。
方案:您已經使用數據庫遷移以編程方式創建了產品屬性。幾個月後,您希望將該屬性從VARCHAR
更改爲TEXT
字段類型。創建後更新Magento產品屬性的數據庫字段類型
如何在保存數據的同時在創建後更改EAV屬性的字段類型?
我的直覺是,這不是通過Magento的安裝類直接支持的,因爲需要觸摸大量的表,需要更新的記錄以及需要從表中複製的內容到桌子。
是的我確認它不被支持。
您可以嘗試更新SQL表,但是這將是一個痛苦...
我會導出所有的產品,應用修改屬性的後端表,並重新導入所有的產品升級腳本。 這種方式magento會自動填充您的屬性使用的新表(catalog_product_entity_text)。
之後,你應該清理你的varchar表中刪除鏈接到您的產品未使用的值(將被永遠不會刪除或更新您的屬性的產品價值現在是TEXT)
我不會假裝這是最漂亮的解決方案四周,我懷疑它的數據庫無關,但這裏是我的解決方案:
<?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();
是的,這是*勢在必行*從'catalog_product_entity_varchar'刪除值 - 否則,從該表中的遺留值將覆蓋UNION select中'catalog_product_entity_text'的值。 – benmarks