Magento正在更改后端中的属性类型

Magento正在更改后端中的属性类型,magento,magento-1.5,Magento,Magento 1.5,是否可以在创建属性后更改其类型。 我想将某些属性的类型更改为多选列表。这些属性的类型当前为“下拉”。 实际上,当我创建属性时,在我最初创建属性时不需要多选,但是,现在客户端希望将其更改为“多选” 请帮助我,我不能通过删除旧属性来创建新属性,因为有一些数据,而设计的某些部分是硬编码的,并且取决于属性的某些值。您可以尝试Mage\u Eav\u Model\u Entity\u Setup::updateAttribute方法。是的,由于方法Mage\u Eav\u Model\u Entity\u

是否可以在创建属性后更改其类型。 我想将某些属性的类型更改为多选列表。这些属性的类型当前为“下拉”。 实际上,当我创建属性时,在我最初创建属性时不需要多选,但是,现在客户端希望将其更改为“多选”


请帮助我,我不能通过删除旧属性来创建新属性,因为有一些数据,而设计的某些部分是硬编码的,并且取决于属性的某些值。

您可以尝试
Mage\u Eav\u Model\u Entity\u Setup::updateAttribute
方法。

是的,由于方法
Mage\u Eav\u Model\u Entity\u Setup::updateAttribute($entityTypeId,$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>

1.0.1
...
2) 您需要在config.xml文件中的标记
之间包含以下代码,请根据您的情况进行调整:

<?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'
));

Mycompany_Mymodule
Mage_Eav_Model_Entity_设置
默认设置
3) 然后,您需要在模块文件夹中创建一个安装脚本,使用新旧版本号app/code/local/mycompany/mymodule/sql/mymodule\u setup/mysql4-upgrade-1.0.0-1.0.php (mysql4升级旧的.version.number新的.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'
));

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'
));

首先,您需要使用下面提到的代码将属性输入类型更新为multiselect:

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表):


在执行此操作时,我还必须设置
'backend\u type'=>'varchar','backend\u model'=>'eav/entity\u attribute\u backend\u array'
@mushin是正确的。您希望在
updateAttribute()
调用中使用
'frontend\u input'
,而不是
'input'
。(在Magento 1.9.0.1上测试)@TylerV。如果您检查class
Mage\u Eav\u Model\u Entity\u Setup::\u prepareValues
您将看到
输入将work@SylvainRayé该方法将接受
输入
,并为您将其更改为
前端输入
。但是,
Mage\u Eav\u Model\u Entity\u Setup::\u prepareValues
仅由
Mage\u Eav\u Model\u Entity\u Setup::addAttribute调用,而不是在说明的第4步中使用的
Mage\u Eav\u Model\u Entity\u Setup::updateAttribute
(或其受保护的对应项)。你在开玩笑吗。magento是一个pos。我尝试了上面的方法,但不适用于我这是我的config.xml:
DELETE FROM catalog_product_entity_int
WHERE entity_type_id = 4 and attribute_id = YOUR_ATTRIBUTE_ID_HERE;