1

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

i am new to magento and dnt have any idea regarding this. Can any body tell me how to do this

4

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

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, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.