0

How can I call model in a custom module? I have a module and I want to create new model in that and use function from model to controller. my model structure is

local>Mypackage>Myenquiry>Model>Mysql4>Myenquiry>Collection.php

There is function colletion in Collection.php. I want to call that function in my controller.But i don't know how to declare this model in config file and how to call it in controller

2 Answers 2

1

Not entirely sure on the bigger picture here but to call a function from your model, you need to declare your module's model in your config.xml:

<config>
<modules>
    <Mypackage_Myenquiry>
        <version>0.1.0</version>
    </Mypackage_Myenquiry>
</modules>
<global>
    ...
    <models>
        <myenquiry>
            <class>Namespace_Module_Model</class>
        </myenquiry>
    </models>
    ...
<global>

One you've declared your model you can then call it in any file like so:

Mage::getModel('myenquiry/myenquiry')->functionName();

From your question it sounds as though your creating your own entity and are wanting to get a collection of that entity, in this case i'm not sure how far you have gotten but you should take a look at some of the following posts/walkthroughs:

Smashing Magazine - How to create an Admin Manageable Entity...

Inchoo - Creating EAV based Model(s) in Magento

How to create an EAV entity

Hope this helps,

Josh

0

You can call Model, like :

//Creating Model instance
$enquiryModel = Mage::getModel("myenquiry/myenquiry");
//Calling Model's function
$enquiryModel->yourFunctionName();

For creating Model, you have to declare it in config.xml, like:

<global>
    <!-- ... -->
    <models>
        <myenquiry>
            <class>Mypackage_Myenquiry_Model</class>
            <!--
            if want to create own resource, use below line.
            Default is core_resource
            -->
            <resourceModel>myenquiry_resource</resourceModel>
        </myenquiry>
    </models>
    <!-- ... -->
</global>

For more about Models, check here : Create Custom Model

Your Answer

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

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