1
votes

How can I call model in a custom module? Lets assume that model is really simple one and returns just arrays of static data.

I have a directory structure similar to this:

  • app
    • local
      • Mypackage
        • Module
          • Models
            • Model1.php

How can I include Model1.php inside controller? If I go with Mage::getModel('Mypackage/Modul/Model1) it returns error since it searches model inside Mage/Module/Model/Model1.php

Thanks!

4
you have to call this way Mage::getModel('Module/Model1).Pankaj Pareek
@Pankaj, but since its a custom package this is not working :(Miha Trtnik
@Bixi can you provide a link where this is explained?Miha Trtnik
@Miha Trtnik : see my answer on other thread : stackoverflow.com/a/13203197/1112003Jscti
@bixi thanks, I was missing xml config another good tutorial I've found after pointed in right direction is here codemagento.com/2011/02/creating-and-instantiating-a-modelMiha Trtnik

4 Answers

1
votes

Mage::getModel('mypackage_module/modeltest) should work. But first check your config.xml. You should have declared it like so:

          <models>
            <mypackage_module>
                <class>Mypackage_Module_Model</class>
                <resourceModel>modeltest_mysql4</resourceModel>
            </mypackage_module>
            <modeltest_mysql4>
                <class>Mypackage_Module_Model_Mysql4</class>
                <entities>
                    <modeltest>
                        <table>mypackage_module</table>
                    </modeltest>
                </entities>
            </modeltest_mysql4>
          </models>
1
votes
Mage::getModel('modulename/modelname')->modelmethod();

i.e.
Mage::getModel('catalog/product')->getName();

Hope this help you

0
votes

This should work: Mage::getModel('module/model_model1')->modelmethod();

Please note that the model folder should be "Model" not "Models"

0
votes

Hello you can try this but I have not tested it

$collection = Mage::getModel('Module/Model1')->getCollection();