2

I have a function abc in a model. I want to call that function abc in the controller.

I am new to Magento and don't have any idea regarding this. Can anybody tell me how to do this?

4 Answers 4

5

It's called Magic Function.

$yourModel = Mage::getModel('yourmodulename/yourmodelname')->load($id);

$return = $yourModel->abc();

or you can do

$yourModel = Mage::getSingleton('yourmodulename/yourmodelname')->load($id);

$return = $yourModel->abc();

this is for your reference of getSingleton VS getModel

1
5

You can use

Mage::getModel('modulename/modulename')->yourMethod();

Suppose you want catalog category then

Mage::getModel('catalog/category')->load($id);
5

Suppose your configuration is -

    <models>
        <modulename> <!--  this here is the first part of your getModel() call -->
            <class>Packagename_Modulename_Model</class>
            <resourceModel>modulename_mysql4</resourceModel>
        </modulename>
        <modulename_mysql4>
            <class>Packagename_Modulename_Model_Mysql4</class>
            <entities>
                <abc>
                    <table>table_abc</table>
                </abc>
            </entities>
        </modulename_mysql4>
    </models>

Now you need the second part after the /. If your model name is: Packagename_Modulename_Model_Abc, then you take everything after your prefix, which was defined in the config Packagename_Modulename_Model, lowercase the first character

Now you can access your model following way:

Mage::getModel('modulename/abc')->yourMethod();
0

If the model lives in a subfolder of your module, you need to load it like:

Mage::getModel('yourmodelname_as_defined_in_config/path_to_file_filename')

Example

Load app/code/local/Aschroder/SMTPPro/Model/Email/Log.php

like Mage::getModel('smtppro/email_log');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.