Search code examples
phpxmlmagento

Can't get <source_model> attribute to work on System.XML in Magento


I'm just finishing off one of my custom shipping methods, which has multiple methods within the main method. So i wanted to have "Allowed Methods" list to show up in Admin section. So I got below block in my system.xml

 <allowed_methods translate="label">
    <label>Allowed Methods</label>
    <frontend_type>multiselect</frontend_type>
    <source_model>mycompany_shipping/carrier_somefolder_definitions_methods</source_model>
    <sort_order>20</sort_order>
    <show_in_default>1</show_in_default>
    <show_in_website>1</show_in_website>
    <show_in_store>0</show_in_store>
    <can_be_empty>1</can_be_empty>
 </allowed_methods>

But Magento cannot find my class because it always look in "Mage" instead of "local", It just throw this error,

include(Mage/Mycompany/Shipping/Model/Carrier/Somefolder/Definitions/Methods.php): 
failed to open stream: No such file or directory  in /Development/trunk/lib/Varien/Autoload.php on line 93

But my class is at local/Mycompany/Shipping/Model/Carrier/Somefolder/Definitions/Methods.php and the class name is Mycompany_Shipping_Model_Carrier_Somefolder_Definitions_Methods

I think may be I missed something in my config.xml, So here is my config.xml

<global>
    <models>
        <mycompshipping>
            <class>Mycompany_Shipping_Model</class>
        </mycompshipping>
    </models>
    <resources>
        <mycompshipping_setup>
            <setup>
                <module>Mycompany_Shipping</module>
            </setup>
            <connection>
                <use>core_setup</use>
            </connection>
        </mycompshipping_setup>
    </resources>
</global>
<default>
    <carriers>
        <mycompanyrate>
            <model>Mycompany_Shipping_Model_Carrier_Mycompanyrate</model>
        </mycompanyrate>
    </carriers>
</default>

Why can't Magento find my class?


Solution

  • If Magento prepends Mage to your classes, it almost always means you have a misconfiguration.

    Glancing at the information you provided, your source model is configured as

    mycompany_shipping/carrier_somefolder_definitions_methods
    

    That's a group name of mycompany_shipping and a class name of carrier_somefolder_definitions_methods. This means Magento will instantiate your source model with a call to

    Mage::getModel('mycompany_shipping/carrier_somefolder_definitions_methods');
    

    However, looking at your config.xml

    <models>
        <mycompshipping>
            <class>Mycompany_Shipping_Model</class>
        </mycompshipping>
    </models>
    

    You've configured your module to "claim" the model group name mycompshipping. This means when you instantiate your module's classes, you use the form

    //instantiates as `Mycompany_Shipping_Model_Carrier_Mycompanyrate`
    Mage::getModel('mycompshipping/carrier_mycompanyrate');
    

    You need to correct your system.xml to instantiate the correct source model, or change config.xml to expose the right group name.