将源模型添加到magento中的属性中


Add source model to attribute in magento

我创建了一个安装脚本,使用下面的脚本向客户添加两个字段。

但我犯了这个错误。

Source model "" not found for attribute "dob_month"

我不是在第一行定义模型吗?那到底有什么作用?解决这个问题的最佳方法是什么?

$setup = new Mage_Eav_Model_Entity_Setup('core_setup');

$setup->addAttribute('customer', 'dob_month', array(
    'label'     => 'Month',
    'type'      => 'varchar',
    'input'     => 'dropdown',
    'visible'   => true,
    'required'  => true,
    'position'  => 1,
    'option'    => array (
        'value' => array (
            'optionone'    => array('January'),
            'optiontwo'    => array('February'),
            'optionthree'  => array('March'),
            'optionfour'   => array('April'),
            'optionfive'   => array('May'),
            'optionsix'    => array('June'),
            'optionseven'  => array('July'),
            'optioneight'  => array('August'),
            'optionnine'   => array('September'),
            'optionten'    => array('October'),
            'optioneleven' => array('November'),
            'optiontwelve' => array('December')
        )
    )
));
$setup->addAttribute('customer', 'dob_year', array (
    'label'     => 'Year',
    'type'      => 'varchar',
    'input'     => 'text',
    'visible'   => true,
    'required'  => true,
    'position'  => 1
));

如果已经添加了属性,则需要使用updateAttributeeav_attribute表中设置源模型值。

<?php
$installer = Mage::getResourceModel('customer/setup','default_setup');
/***
 * When working with EAV entities it's important to use their module's setup class.
 * See Mage_Customer_Model_Resource_Setup::_prepareValues() to understand why.
 */
$installer->startSetup();
$installer->updateAttribute(
    'customer',
    'dob_month',
    'source_model', //a.o.t. 'source'
    'whatever/source_model',
)
$installer->endSetup();

如果不是,那么您可以使用addAttribute(),由于Mage_Eav设置类的_prepareValues()方法,它需要一个别名作为source_mode列,如Alexei的回答所示("source"与"source_mode"相对)。

当Magento需要知道属性的可能值时,将使用源模型。例如,渲染属性的下拉列表时。所以,不,你没有定义它。如果我的内存没有让我失望,你可以通过在属性定义数组中添加"source"来实现。类似于:

...
'source' => 'eav/entity_attribute_source_table'
...

这意味着所有可能的选项都存储在表eav_attribute_option和eav_aattribute_option中。所以,如果安装脚本中的选项被成功添加到这些表中,那应该会起作用。或者你可以写你自己的源代码模型,我更喜欢它。