向客户实体添加属性

37
我的当前目标是添加一个新的客户属性(类型为int),它应该显示为选择框,其中包含预定义选项(从具有可在后端编辑条目的模型加载,这已经完成)。 我正在努力正确使用$installer->addAttribute()方法,特别是指定正确的源选项。另一个问题是新属性未保存到eav_entity_attribute表中。 我使用的是Magento CE 1.5.1.0版本。
4个回答

70

这是一个基本的int属性和text渲染器的代码:

$installer = $this;
$installer->startSetup();

$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->addAttribute('customer', 'your_attribute_code_here', array(
    'input'         => 'text',
    'type'          => 'int',
    'label'         => 'Some textual description',
    'visible'       => 1,
    'required'      => 0,
    'user_defined' => 1,
));

$entityTypeId     = $setup->getEntityTypeId('customer');
$attributeSetId   = $setup->getDefaultAttributeSetId($entityTypeId);
$attributeGroupId = $setup->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);

$setup->addAttributeToGroup(
 $entityTypeId,
 $attributeSetId,
 $attributeGroupId,
 'your_attribute_code_here',
 '999'  //sort_order
);

$oAttribute = Mage::getSingleton('eav/config')->getAttribute('customer', 'your_attribute_code_here');
$oAttribute->setData('used_in_forms', array('adminhtml_customer'));
$oAttribute->save();

$setup->endSetup();

为添加属性而采取的不寻常步骤是使用setData('used_in_forms'), 这似乎对于客户属性是唯一的。如果没有它,该字段将不会被渲染,特别是在管理html中。你可以在customer_form_attribute数据库表中查看此数组的有效选项。

至于使用具有预定义选项的select,这就是你需要的:

$iAttributeId = $installer->getAttributeId($entityTypeId, 'your_attribute_code_here');
$aClasses = array('TV','DVD','Home Theatre','Air Conditioner','Stereo/Hifi','Game Console','Camcorder','VCR','Set Top Box','PVR');
$aOption = array();
$aOption['attribute_id'] = $iAttributeId;

for($iCount=0;$iCount<sizeof($aClasses);$iCount++){
    $aOption['value']['option'.$iCount][0] = $aClasses[$iCount];
}
$setup->addAttributeOption($aOption);

这里有一个演示,介绍如何使用自定义数据源来创建下拉菜单。

希望对您有所帮助,
JD


刚试了一下你的代码,运行得非常好,addAttributeToGroup调用真的很有帮助。我还设置了源选项。 - Zifius
7
注意 sort_order 字段: 像这样的代码,我总是在数据库中得到 sort_order = 0,花费了我几个小时进行调试才发现问题所在:addAttributeToGroup() 函数运行正常,但是 $oAttribute->save() 函数会再次覆盖 sort_order 值,实际上执行了以下查询: INSERT INTO customer_form_attribute (form_code,attribute_id) VALUES('adminhtml_customer',159); UPDATE eav_entity_attribute SET sort_order = 0 WHERE (attribute_id='159') 解决方法: 在保存属性之前设置 sort_order 值: $oAttribute->setData('sort_order', $sortOrder); - Fabian Schmengler
谢谢@Fabian,我注意到了排序问题,但由于其他一切都正常,所以没有去检查为什么会出现这种情况。 - Zifius
3
如果您需要帮助了解如何将此安装信息添加到您的安装中,我建议使用这篇文章:http://alanstorm.com/magento_setup_resources - Chris
我在服务器上的Magento Enterprise 1.12上遇到了以下错误。 在本地主机上运行良好,但在服务器上成为一场噩梦。 在我删除 $setup = new Mage_Eav_Model_Entity_Setup('core_setup');之后,一切都正常工作了。 我将其更改为: $setup = $this; $setup->startSetup();
安装错误:异常“Exception”,消息为“注意:在/var/www/vhosts/mage/app/code/core/Mage/Core/Model/Resource/Setup.php的第139行尝试获取非对象属性”/var/www/vhosts/mage/app/code/core/Mage/Core/functions.php:245”。
- Hashid Hameed
显示剩余7条评论

23

@Jonathan Day的回答很好,对我帮助很大。但是,只要将你的setup类设置为Mage_Customer_Model_Entity_Setup,那么Magento就可以为你完成所有这些工作:

<!-- config.xml Example -->
<?xml version="1.0"?>
<config>
    <global>
        <resources>
            <acme_module_setup>
                <setup>
                    <module>Acme_Module</module>
                    <class>Mage_Customer_Model_Entity_Setup</class>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </acme_module_setup>
        </resources>
    </global>
</config>

这里是 mysql4-install-X.X.X.php 文件:

<?php

$installer = $this;
/* @var $installer Mage_Customer_Model_Entity_Setup */

$installer->startSetup();

$installer->addAttribute(
    'customer',
    'acme_imported',
    array(
        'group'                => 'Default',
        'type'                 => 'int',
        'label'                => 'Imported into Acme',
        'input'                => 'select',
        'source'               => 'eav/entity_attribute_source_boolean',
        'global'               => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
        'required'             => 0,
        'default'              => 0,
        'visible_on_front'     => 1,
        'used_for_price_rules' => 0,
        'adminhtml_only'       => 1,
    )
);

$installer->endSetup();

adminhtml_only 将为您处理所有的 used_in_forms 逻辑。同时,定义 group 将负责将其分配到属性组。


3
在使用1.7.0.2 CE版本时,adminhtml_only 技巧对我无效。我仍然需要添加以下代码:Mage::getSingleton( 'eav/config' )->getAttribute( 'customer', $custom_attribute_id )->setData( 'used_in_forms', array( 'adminhtml_customer' ) )->save() 才能在后端显示属性。 - Louis B.

4

您需要通过以下脚本在自定义模块的mysql设置文件下添加您的客户属性。

$installer = $this;
$installer->startSetup();


$installer->addAttribute("customer", "yourattributename",  array(
    "type"     => "int",
    "backend"  => "",
    "label"    => "Bad Customer",
    "input"    => "select",
    "source"   => "eav/entity_attribute_source_boolean",
    "visible"  => true,
    "required" => false,
    "default" => "",
    "frontend" => "",
    "unique"     => false,
    "note"       => ""

    ));

        $attribute   = Mage::getSingleton("eav/config")->getAttribute("customer", "yourattributename");

以下脚本用于在想要使用客户属性的地方:

以下脚本用于需要使用客户属性的地方

$used_in_forms=array();

$used_in_forms[]="adminhtml_customer";
        $attribute->setData("used_in_forms", $used_in_forms)
        ->setData("is_used_for_customer_segment", true)
        ->setData("is_system", 0)
        ->setData("is_user_defined", 1)
        ->setData("is_visible", 0)
        ->setData("sort_order", 100)
        ;
        $attribute->save();

$installer->endSetup();

0

alex和leek提供的解决方案都对我有用。只是我需要在我们的AccountController.php中添加setter函数。

$customer->setProfession($this->getRequest()->getPost('profession')) 
                        ->save(); // Added for updating Profession

其中,“profession”是我的自定义属性。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接