【问题标题】:Magento: How do I properly install a custom attribute into the DB tables?Magento:如何将自定义属性正确安装到数据库表中?
【发布时间】:2012-08-15 23:21:20
【问题描述】:

我一直在对我的 magento 安装进行一些修改以使其符合我的需要,但我遇到了很多问题。这是我所做的一些事情,以防万一需要它作为我即将提出的问题的参考。

Magento: Add New Field to Credit Card Form

Magento: How to display customer's phone number on customer information field

Magento: How to Add Order / Payment information to Admin-BackEnd

Magento: How to save data to order_payment and quote_payment

所以我遵循了一些关于添加自定义属性的教程,但我对如何运行脚本将属性添加到正确的表感到困惑。

这就是为什么。

我注意到,magento 自带的“电话”字段出现在几个表上。

eav_attributes (4 个实例) 然后电话字段的属性 ID 也出现在其他表中,所有 4 个实例。现在,我添加了一个移动电话号码字段,如我之前的问题中所见,但我不知道如何使它出现在所有这些表格中并具有必要的值。

我现在提供更详细信息的资源有点有限,但如果有人能理解我要解释的内容,请帮助我。

谢谢。

【问题讨论】:

    标签: magento


    【解决方案1】:

    回答

    • 使用实体模块的设置资源类执行的设置脚本。

    为什么?

    虽然存在所有 EAV 属性和实体共有的类和表(例如 eav_attribute 表和 Mage_Eav_Model_Entity_Setup),但每个 EAV 实体可能有一个附加的属性信息表。通用设置资源类Mage_Eav_Model_Entity_Setup 有一个_prepareValues() 方法,它为所有通用属性元数据(即eav_attribute 表中的列)设置默认值。对于Mage_Customer,有customer_eav_attribute 表等。 Mage_Customer 设置资源类 Mage_Customer_Model_Entity_Setup 覆盖此方法,调用父方法并添加 更多 存储在 customer_eav_attribute 表中的元数据默认值。

    因此,虽然设置资源文件可以在任何上下文(设置类实例)中执行,但如果它正在修改其他模块的 EAV 配置,则需要使用正确的类。

    <?php
    
    $installer = Mage::getResourceModel('customer/setup','customer_setup');
    
    $installer->startSetup();
    
    $installer->addAttribute(
        'customer',
        'new_attribute_code',
        array('label'=>'Label','type' => 'text')
    )
    
    $installer->endSetup();
    

    根据添加到客户/客户地址表格的需要,还有进一步的工作要做。此类工作的参考资料可以在Mage/Customer/sql/customer_setup/ 中找到。

    【讨论】:

    • 非常感谢您的回复。我将为必要的属性编写一个设置脚本,并试一试,希望它能奏效。从数据库中手动添加/删除值并不是我想要做的事情,但不太确定什么是最好的方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-13
    相关资源
    最近更新 更多