2

I have a design like below

enter image description here

I have two independent modules and one common module. That means independent module will pull information from common module.

I defined below in common module config.

<config>
    <global>
        <models>
            <services>
                <class>My_Common_Model</class>
            <services>
        <models>
    </global>
</config>

Then I tried to call a method in common module model from module_1 controller like below,

$stores = Mage::getModel('services/store')->getAvailableStore();

But is throws error like model undefined.

Then I included the common model configuration in Module_1 config.xml also. Its working now.

Is it the normal behavior? Cant we call any method in one modules model from other modules controller with defined tag in its native module itself?

I don't know how to ask this question better than this.

Module 1 Controller:

<?php
    class My_Module_UserController extends Mage_Core_Controller_Front_Action{

        function userAssignStoreAction(){

            $stores = Mage::getModel('stores/store')->getAvailableStore();

            echo $productModel;
            die;
        }
    }
?>

below is common module config file:

<config>
    <modules>
        <My_Common>
            <version>0.0.1</version>
        </My_Common>
    </modules>
    <global>
        <models>
            <stores>
                <class>My_Common_Model</class>
            </stores>
        </models
    </global>
</config>

below is module 1 config:

<global>
        <models>
            <services>
                <class>My_Module_Model</class>
            </services>
           <!-- WITHOUT BELOW DECLARATION UNABLE TO CALL COMMON MODEL METHOD FROM MODULE 1 CONTROLLER -->
            <stores>
                <class>My_Common_Model</class>
            </stores>
        </models>
    </global>

2 Answers 2

2

You should be able to call your custom model from anywhere in Magento, the same way you call any other core model e.g Mage::getModel('catalog/product')

Did you check to make sure module_1 is active?

5
  • Yes the module 1 is active, But unable to call like Mage::getModel('catalog/product') without defining it again the common module model in module 1's config
    – Anto S
    Aug 25, 2015 at 12:26
  • Can you add some code for the config.xml and the controller Aug 25, 2015 at 12:28
  • I added code snippets
    – Anto S
    Aug 25, 2015 at 12:35
  • I think the first step would be to figure out why core module is not working. I would start by disable all other custom module Aug 25, 2015 at 12:45
  • I found the error, <models> tag not closed properly. Look at the common module config. Thanks for your support +1.
    – Anto S
    Aug 25, 2015 at 12:50
0

Yes, What u did is perfect. Actually, This is the way magento works. Go for it. For example.

This function is defined on mage/checkout/cart.php

You can see that Mage::getModel('catalog/product') in this function. The thing is, Its okay to call other models in our own module too.

Hope you got it. Go ahead.! Cheers.

public function addOrderItem($orderItem, $qtyFlag=null)
{
    /* @var $orderItem Mage_Sales_Model_Order_Item */
    if (is_null($orderItem->getParentItem())) {
        $product = Mage::getModel('catalog/product')
            ->setStoreId(Mage::app()->getStore()->getId())
            ->load($orderItem->getProductId());
        if (!$product->getId()) {
            return $this;
        }

        $info = $orderItem->getProductOptionByCode('info_buyRequest');
        $info = new Varien_Object($info);
        if (is_null($qtyFlag)) {
            $info->setQty($orderItem->getQtyOrdered());
        } else {
            $info->setQty(1);
        }

        $this->addProduct($product, $info);
    }
    return $this;
}

Your Answer

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

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