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

Sco*_*t S 1 php magento

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

但是我得到了这个错误.

Source model "" not found for attribute "dob_month"
Run Code Online (Sandbox Code Playgroud)

我没有在第一行定义模型吗?这究竟是做什么的?解决这个问题的最佳方法是什么?

$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
));
Run Code Online (Sandbox Code Playgroud)

ben*_*rks 5

如果您已添加该属性,则需要使用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();
Run Code Online (Sandbox Code Playgroud)

如果没有,那么你可以使用addAttribute()- 由于Mage_Eavsetup类_prepareValues()方法 - 需要source_model列的别名,如Alexei的答案所示('source'而不是'source_model').