错误 :- 在非对象洋红色上调用成员函数 toOptionArray()


Error :- Call to a member function toOptionArray() on a non-object magento

我在Magento中创建了新的付款方式。我在管理面板中遇到以下错误。(在非对象上调用成员函数 toOptionArray() )。我用谷歌搜索了最后我得到的只有糟糕的解决方案(核心文件中的代码更改)。

 I need to fix the issue without change core file code .
   if(is_object($sourceModel)){
    $optionArray = $sourceModel->toOptionArray($fieldType == 'multiselect');
   } else {
      Mage::log($e->source_model);
   }

从提供的信息中,我可以猜测您在系统配置中缺少选择字段的源模型(或者未正确指定此源模型)

首先检查模块的系统.xml文件 - 每个选择字段都应指定源模型:

<fields>
    <my_select_field>
        <label>My Label</label>
        <frontend_type>select</frontend_type>
        <source_model>your_models_name/path_to_class</source_model>
        <sort_order>0</sort_order>
        <show_in_default>1</show_in_default>
        <show_in_website>0</show_in_website>
        <show_in_store>0</show_in_store>
    </my_select_field>
</fields>

其次,检查源模型类是否存在(并且命名正确)。

作为源模型声明和实现的示例,您可以使用指向app/code/core/Mage/Adminhtml/Model/System/Config/Source/Yesno的"adminhtml/system_config_source_yesno.php

请注意,模型位置是在配置中配置的.xml在全局/模型/your_models_name/类节点下

顺便说一句,如果您的选择表示布尔设置,则可以使用"adminhtml/system_config_source_yesno"而无需在模块中实现源模型。