1

以下のスクリプトを使用して、顧客に 2 つのフィールドを追加するインストール スクリプトを作成しました。

しかし、私はこのエラーが発生します。

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

2 に答える 2

5

属性を既に追加している場合は、 を使用updateAttributeしてテーブルにソース モデルの値を設定しeav_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_model 列のエイリアスが必要です (「source_model」ではなく「source」)。

于 2012-08-22T01:24:02.027 に答える
3

ソースモデルは、Magento が属性の可能な値を知る必要がある場合に使用されます。たとえば、属性のドロップダウンをレンダリングするとき。いいえ、あなたはそれを定義していません。私の記憶が私に失敗しない場合は、属性定義配列に「ソース」を追加することでそれを行うことができます。何かのようなもの:

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

これは、可能なすべてのオプションがテーブル eav_attribute_option および eav_attribute_option に格納されることを意味します。したがって、インストール スクリプトからのオプションがこれらのテーブルに正常に追加された場合、それは機能するはずです。または、独自のソース モデルを作成することもできます。

于 2012-08-21T21:45:58.030 に答える