向客户实体添加属性

向客户实体添加属性

本文介绍了向客户实体添加属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

my current goal is to add a new customer attribute (with int type) which should appear as select with predefined options (loaded from a model with entries editable in backend, which is done).I'm struggling with proper use of $installer->addAttribute() method, especially specifying correct source option. Other problem is the new attribute isn't saved to eav_entity_attribute table

我使用的是Magento CE 1.5.1.0

I'm on Magento CE 1.5.1.0

推荐答案

这是带有text渲染器的基本int属性的代码:

This is the code for a basic int attribute with text renderer:

$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数据库表中看到此阵列的有效选项.

The unusual step for adding attributes is the setData('used_in_forms') this seems to be unique to customer attributes. Without it, the field won't get rendered, certainly not in the adminhtml anyway. You can see the valid options for this array in the customer_form_attribute database table.

关于将select与预定义选项一起使用,这是您需要的:

In terms of using a select with predefined options, this is what you need:

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

这是演练关于为下拉菜单使用自定义来源

And here is a walk-through on using a custom source for your drop-down

希望这会有所帮助,
京东

Hope this helps,
JD

这篇关于向客户实体添加属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 06:29