向客户实体添加属性

Zif*_*ius 37 php custom-attributes magento

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

我在Magento CE 1.5.1.0上

Jon*_*Day 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();
Run Code Online (Sandbox Code Playgroud)

添加属性的不寻常步骤是,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);
Run Code Online (Sandbox Code Playgroud)

这里是一个步行通过使用自定义源为您的下拉

希望这有帮助,
JD

  • **注意sort_order:**使用类似的代码我总是在DB中得到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);` (7认同)
  • 如果您需要帮助知道如何将此设置信息添加到您的安装,那么我建议使用这篇文章:http://alanstorm.com/magento_setup_resources (3认同)

lee*_*eek 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>
Run Code Online (Sandbox Code Playgroud)

这是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();
Run Code Online (Sandbox Code Playgroud)

adminhtml_only上述将处理所有的used_in_forms逻辑你.此外,定义group将负责将其分配给属性组.

  • 使用1.7.0.2 CE,*adminhtml_only*技巧对我不起作用.我仍然需要添加:`Mage :: getSingleton('eav/config') - > getAttribute('customer',$ custom_attribute_id) - > setData('used_in_forms',array('adminhtml_customer')) - > save()`使属性显示在后端. (3认同)