1

シナリオ: データベース移行を使用してプログラムで製品属性を作成しました。VARCHAR数か月後、その属性を aからTEXTフィールド タイプに変更したいと考えています。

作成後にデータを保持しながら EAV 属性のフィールド タイプを変更するにはどうすればよいですか?

私の直感では、触れる必要のある無数のテーブル、更新する必要があるレコード、およびテーブルからテーブルにコピーする必要があるコンテンツのため、これは Magento のセットアップ クラスを通じて直接サポートされていないということです。

4

2 に答える 2

4

これが最も美しい解決策であると主張するつもりはありません。データベースに依存しないとは思えませんが、私の解決策は次のとおりです。

<?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();
于 2013-05-27T13:55:21.780 に答える
2

はい、サポートされていないことを確認しました。

SQLでテーブルを更新しようとすることもできますが、それは面倒です...

すべての製品をエクスポートし、属性バックエンド テーブルを変更するアップグレード スクリプトを適用して、すべての製品を再インポートします。そうすれば、magento は、属性で使用される新しいテーブル (catalog_product_entity_text) を自動的に埋めます。

その後、varchar テーブルを消去して、製品にリンクされた未使用の値を削除する必要があります (属性の製品が TEXT になったため、削除も更新もされない値)。

于 2013-05-27T12:48:41.110 に答える