Magento Call a function of Model in Controller
-
16-10-2019 - |
سؤال
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
المحلول
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
نصائح أخرى
You can use
Mage::getModel('modulename/modulename')->yourMethod();
Suppose you want catalog category then
Mage::getModel('catalog/category')->load($id);
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();
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');