向客户实体添加属性

问题描述 投票:37回答:4

我目前的目标是添加一个新的客户属性(使用int类型),该属性应该显示为具有预定义选项的select(从具有可在后端编辑的条目的模型加载,这已完成)。我正在努力正确使用$installer->addAttribute()方法,特别是指定正确的源选项。其他问题是新属性未保存到eav_entity_attribute表

我在Magento CE 1.5.1.0上

php magento custom-attributes
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'),这似乎是客户属性所特有的。没有它,该字段将不会被渲染,当然不会在adminhtml中。您可以在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);

这是一个walk-through使用自定义源下拉列表

希望这可以帮助, JD


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将负责将其分配给属性组。


4
投票

您只需通过以下脚本通过自定义模块mysql安装文件添加您的customer属性。

$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");

以下脚本用于想要使用customer属性的位置

$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

“职业”是我的自定义属性。

© www.soinside.com 2019 - 2024. All rights reserved.