2011-10-21 59 views
9

是否可以在創建屬性後更改屬性的類型。 我想將某些屬性的類型更改爲多選列表。這些屬性的類型目前是「下拉菜單」。 實際上,當我創建屬性時,最初創建屬性時不需要多重選擇,但現在客戶端想要將其更改爲「多選」。Magento在後臺更改屬性類型

請幫助我,我不能通過刪除舊的屬性來創建新的屬性,因爲有一些數據,並且設計的某個部分是硬編碼的並且取決於屬性的某些值。

回答

17

是的,它可以通過編程方式感謝方法Mage_Eav_Model_Entity_Setup::updateAttribute($entityTypeId, $id, $field, $value=null, $sortOrder=null)

這是不可能的,在Magento後端屬性管理,因爲它與現有的數據結果。在你的情況下,從select更改爲multiselect應該沒問題,但要做數據庫備份並測試你的產品是否仍然正確顯示。

以編程方式,最好的方法是從更新設置腳本執行它。我不知道你的模塊,但這裏有一些信息來做到這一點。

當您向模塊提供新的號碼版本時,會啓動更新設置腳本,並提供新舊版本號作爲文件名的安裝腳本。

1)以下是config.xml模塊的標題,將其更改爲提供更高版本。例如,新版本

<?xml version="1.0" encoding="UTF-8"?> 
<config> 
<modules> 
    <Mycompany_Mymodule> 
     <version>1.0.1</version><!-- the old one was 1.0.0 --> 
    </Mycompany_Mymodule> 
</modules> 
... 
</config> 

2)你必須config.xml文件,標籤<global>...</global>下面的代碼之間,請適應您的情況:

<resources> 
     <mymodule_setup><!-- name that you will give to the folder into the sql folder --> 
      <setup> 
       <module>Mycompany_Mymodule</module> 
       <class>Mage_Eav_Model_Entity_Setup</class><!-- You can have a setup class which extends this class --> 
      </setup> 
      <connection> 
       <use>default_setup</use> 
      </connection> 
     </mymodule_setup> 
    </resources> 

3)然後你需要在你的模塊文件夾中創建一個新舊版本號爲app/code/local/mycompany/mymodule/sql/mymodule_setup/mysql4-upgrade-1.0.0-1.0.1.php的安裝腳本 (mysql4-upgrade-old.version.number-new.version.number.php)

4),並設置一個這樣的代碼這一新的腳本,請適應您的情況:

<?php 
$installer = $this; 
/*@var $installer Mage_Eav_Model_Entity_Setup */ 

$entityTypeId = $installer->getEntityTypeId('catalog_product'); 

$idAttributeOldSelect = $this->getAttribute($entityTypeId, 'myold_attribute', 'attribute_id'); 
$installer->updateAttribute($entityTypeId, $idAttributeOldSelect, array(
    'frontend_input' => 'multiselect' 
)); 

5)刷新您的Magento的頁面,並最終刷新緩存

+0

感謝您的詳細解答,我會嘗試您提出的解決方案... – Ravish

+0

@mushin是正確的。你想在'updateAttribute()'調用中調用''frontend_input'',而不是''input''。 (在Magento 1.9.0.1上測試) –

+0

@TylerV。如果你檢查「Mage_Eav_Model_Entity_Setup :: _ prepareValues」類,你會看到'input'將會工作 –

0

您可以嘗試Mage_Eav_Model_Entity_Setup::updateAttribute方法。

6

4,我想更新使用數據庫字段,即輸入應該frontend_input。

<?php 
$installer = $this; 
/*@var $installer Mage_Eav_Model_Entity_Setup */ 

$entityTypeId = $installer->getEntityTypeId('catalog_product'); 

$idAttributeOldSelect = $this->getAttribute($entityTypeId, 'myold_attribute', 'attribute_id'); 
$installer->updateAttribute($entityTypeId, $idAttributeOldSelect, array(
    'frontend_input' => 'multiselect' 
)); 
+0

當下面這個我也必須設置''backend_type'=>'varchar','backend_model'=>'eav/entity_attribute_backend_array'' – clockworkgeek

+0

I試過這不起作用? –

0

首先,你需要更新屬性輸入類型,使用下述代碼,多選:

UPDATE eav_attribute SET 
entity_type_id = '4', 
attribute_model = NULL, 
backend_model = 'eav/entity_attribute_backend_array', 
backend_type = 'varchar', 
backend_table = NULL, 
frontend_model = NULL, 
frontend_input = 'multiselect', 
frontend_class = NULL 
WHERE attribute_id = 'YOUR_ATTRIBUTE_ID_HERE'; 

如今,從舊錶複製屬性值到新:

INSERT INTO catalog_product_entity_varchar (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_int 
WHERE attribute_id = YOUR_ATTRIBUTE_ID_HERE; 

最後,刪除舊值,否則它們將與新設置發生衝突(舊值將加載,但Magento會將新值保存到varchar表中):

DELETE FROM catalog_product_entity_int 
WHERE entity_type_id = 4 and attribute_id = YOUR_ATTRIBUTE_ID_HERE;