向客户实体添加属性

发布于 2024-11-06 05:38:25 字数 227 浏览 1 评论 0原文

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

我使用的是 Magento CE 1.5.1.0

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

I'm on Magento CE 1.5.1.0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

や三分注定 2024-11-13 05:38:25

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

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

这是一个 演练为您的下拉菜单使用自定义源

希望这会有所帮助,
京东

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

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.

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

遗忘曾经 2024-11-13 05:38:25

@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 -XXXphp 文件:

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

@Jonathan Day's answer is great and helped me tremendously. However - as long as you've set your setup class to Mage_Customer_Model_Entity_Setup, then Magento can do all of that work for you:

<!-- 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>

And here is the mysql4-install-X.X.X.php file:

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

The adminhtml_only above will handle all of the used_in_forms logic for you. Also, defining group will take care of assigning it to the attribute group.

只怪假的太真实 2024-11-13 05:38:25

只需通过以下脚本在自定义模块 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();

Just you have add your customer attribute under by your custom module mysql setup file through the following script.

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

The following script used for where want to use customer attribute

$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();
陈甜 2024-11-13 05:38:25

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

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

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

The solution provide by alex and leek both worked for me.
Only I have to add the setter function in our AccountController.php

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

Where "profession" was my custom attribute.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文