【问题标题】:Adding attributes to customer entity向客户实体添加属性
【发布时间】:2011-05-11 08:24:11
【问题描述】:

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

我正在使用 Magento CE 1.5.1.0

【问题讨论】:

    标签: php magento custom-attributes


    【解决方案1】:

    这是带有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);
    

    这里是walk-through 为您的下拉菜单使用自定义源

    希望这会有所帮助,
    京东

    【讨论】:

    • 感谢您的回复,我会尝试一下。至于预定义的选项,在我的情况下它们不应该是静态的,而是可以从管理界面编辑,这就是我尝试使用 source 选项的原因
    • 编辑了动态选项的链接。如果您使用的是提供客户属性管理器的 Magento 版本,我的预定义选项示例可以在管理员中进行编辑,这只是一个起始集。
    • 刚刚尝试了你的代码,它的工作就像一个魅力,addAttributeToGroup 调用真的很有帮助。我还设置了源选项。
    • 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-&gt;setData('sort_order', $sortOrder);
    • 如果您需要帮助了解如何将此设置信息添加到您的安装中,那么我建议您使用这篇文章:alanstorm.com/magento_setup_resources
    【解决方案2】:

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

    【讨论】:

    • 使用 1.7.0.2 CE,adminhtml_only 技巧对我不起作用。我仍然必须添加:Mage::getSingleton( 'eav/config' )-&gt;getAttribute( 'customer', $custom_attribute_id )-&gt;setData( 'used_in_forms', array( 'adminhtml_customer' ) )-&gt;save() 以使属性显示在后端。
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-27
        相关资源
        最近更新 更多