I have added a menu option in the system | configuration panel but when I click on the link I get the below error
Fatal error: Call to a member function toOptionArray() on a non-object in /Users/myName/Sites/magentoDevTest/app/code/core/Mage/Adminhtml/Block/System/Config/Form.php on line 463
I was hoping to try and solve this problem by looking at the core code, but I have not been successful with that approach, Ive looked around online but cant finds anything that has helped me
My system.xml file is
<config>
<tabs><!--creates tabs on the adminhtml page-->
<training translate="label" module="training">
<label>Training</label><!--creates a tab with the title of Training (a menu's title is called title a tabs title is called training)-->
<sort_order>102</sort_order><!--the order of the tab-->
</training>
</tabs>
<sections><!--creates a section on under the tab called animal-->
<training translate="label" module="training"><!--maps to <config><tabs><training> in this xml doc (system.xml)-->
<label>Animal</label><!--names the bar animal-->
<sort_order>10</sort_order><!--order of the bar-->
<tab>training</tab><!--maps to <config><tabs><training> must be the same casing as the tab it maps to-->
<show_in_default>1</show_in_default><!--shows this tab in a website scope-->
<show_in_website>1</show_in_website><!--shows this tab in a website scope-->
<show_in_store>1</show_in_store><!--shows this tab in a website scope-->
<groups><!--sets the blue bar in the main content area of the page-->
<general translate="label" module="training">
<label>General Settings</label><!--sets the blue bars title-->
<sort_order>10</sort_order><!--sets the order of the bar-->
<show_in_default>1</show_in_default><!--shows this tab in a website scope-->
<show_in_website>1</show_in_website><!--shows this tab in a website scope-->
<show_in_store>1</show_in_store><!--shows this tab in a website scope-->
<fields><!--sets which fields are available in the "general" bar-->
<recommendation translate="label comment" module="training">
<label>Recommendation</label><!--creates a option called recommentaion-->
<frontend_type>text</frontend_type><!--What does this do?-->
<sort_order>10</sort_order><!--sets the sort order of this field-->
<show_in_default>1</show_in_default><!--shows this tab in a website scope-->
<show_in_website>1</show_in_website><!--shows this tab in a website scope-->
<show_in_store>1</show_in_store><!--shows this tab in a website scope-->
<depends><!--This refers to <show_recommendations> node and is required for <show_recommendations> to be displayed-->
<show_recommendations>1</show_recommendations>
</depends>
<comment><![CDATA[<strong>This</strong> is a comment]]></comment><!--what does this do?-->
</recommendation>
<show_recommendation translate="label" module="training">
<label>Display Recommendations</label><!--creates a option called display recommentaion-->
<frontend_type>select</frontend_type><!--whats this do?-->
<source_model>training/system_config_source_show</source_model><!--sets the source model tha will be used?-->
<sort_order>20</sort_order><!--sets the order-->
<show_in_default>1</show_in_default><!--shows this tab in a website scope-->
<show_in_website>1</show_in_website><!--shows this tab in a website scope-->
<show_in_store>1</show_in_store><!--shows this tab in a website scope-->
<tooltip>More Javascript in use!</tooltip><!--shows text on hover-->
</show_recommendation>
</fields>
</general>
</groups>
</training>
</sections>
===EDIT===
config.xml
<global>
<models>
<training><!--Namespace name-->
<class>Training_Animal_Model</class><!--File path to the model directory-->
<resourceModel>training_animal_resource</resourceModel><!--maps to resource model node-->
</training>
...
-
Please also post your config.xml, especially the part that includes your models (inside the global tag)Sander Mangel– Sander Mangel2013年09月09日 13:24:11 +00:00Commented Sep 9, 2013 at 13:24
4 Answers 4
Most probably this is the issue :
<source_model>training/system_config_source_show</source_model>
When declaring a source model for a field in system.xml, Magento will try to retrieve the options for the field by calling:
Mage::getModel('source model here')->toOptionArray();
So in your case it tries :
Mage::getModel('training/system_config_source_show')->toOptionArray();
and Mage::getModel('training/system_config_source_show') returns null. Make sure the model exists.
-
That's defiantly the problem (i removed the <depends> node and <show_recommendation> to confirm). I have looked in config.xml and my model is defined in there. So do I need a file path of system_config_source_show.php in my models folder? if so what am I extending? I have added the relevant part of my config.xmltony09uk– tony09uk2013年09月09日 13:42:01 +00:00Commented Sep 9, 2013 at 13:42
-
1It's not important what you are extending. You can even have a class that does not extend anything. Just make sure you have the method
toOptionArray()that returns an array (Duh!). array('value1'=>'label1', 'value2'=>'label2')`Marius– Marius2013年09月09日 14:41:08 +00:00Commented Sep 9, 2013 at 14:41 -
Just a notice: I've frequently had the same issue when someone installed one of my modules, but forgot to refresh the Magento Compiler or flush the PHP accelerator cache (OPCache or APC when the check-filestamp option was off). I'm quite sure it is not related to the original post, but it might be useful to people who end up here through search ;)Jisse Reitsma– Jisse Reitsma2015年02月13日 17:24:00 +00:00Commented Feb 13, 2015 at 17:24
copy app\code\core\Mage\Adminhtml\Block\System\Config\Form.php
paste / extend to / create app\code\local\Mage\Adminhtml\Block\System\Config\Form.php
find the following on line 463
$optionArray = $sourceModel->toOptionArray($fieldType == 'multiselect');
and replace it with:
if(is_object($sourceModel)){
$optionArray = $sourceModel->toOptionArray($fieldType == 'multiselect');
} else {
Mage::log($e->source_model);
}
-
So this fixes my issue but doesn't seem to be logging anything. When I revert, it goes back to a blank screen and the same error as above. Why do you think it wouldn't be logging?sparecycle– sparecycle2015年02月17日 19:39:16 +00:00Commented Feb 17, 2015 at 19:39
-
Have the same issue with a 3rd party module. Can confirm this answer provides a work-around. Logfile still gives no clue, just complains that $e is undefined as well:
2015年12月18日T19:55:04+00:00 ERR (3): Notice: Undefined variable: e in /var/www/dev/htdocs/app/code/core/Mage/Adminhtml/Block/System/Config/Form.php on line 466 2015年12月18日T19:55:04+00:00 ERR (3): Notice: Trying to get property of non-object in /var/www/dev/htdocs/app/code/core/Mage/Adminhtml/Block/System/Config/Form.php on line 466If someone knows how to further troubleshoot this, feel free to add a comment or an answer.Ottonet– Ottonet2015年12月18日 20:11:56 +00:00Commented Dec 18, 2015 at 20:11 -
It's a bad practice to modify the core.Nolwennig– Nolwennig2016年01月19日 14:14:26 +00:00Commented Jan 19, 2016 at 14:14
-
Hi @Nolwennig just paste / extend to / create app\code\local\Mage\Adminhtml\Block\System\Config\Form.phpAndhi Irawan– Andhi Irawan2016年07月19日 04:10:15 +00:00Commented Jul 19, 2016 at 4:10
In my case, I solved it with the following steps:-
- Disable Compilation (
System -> Tools -> Compilation) - Refresh Cache (
System -> Cache Management)
Create file in app/code/local/Training/Animal/Model/System/Config/Source/Show.php
<?php
class Training_Animal_Model_System_Config_Source_Show {
public function toOptionArray() {
return array(
array('value' => 0, 'label'=>Mage::helper('training')->__('Load all recommendation')),
array('value' => 1, 'label'=>Mage::helper('training')->__('Show only available recommendation')),
);
}
}