I have developed a custom module which includes three DB tables. I need to fetch values from one table and then based on that value fetch values from second table.

For ex below is the table description:

**deal_offers** : entity_id | deal_name | deal_discount

**deal_products** : deal_id | product_id

In my observer, I am getting product_id value. Now for say product_id, what I want to achieve is

  1. First, fetch deal_id value from table deal_products, using the value of product_id

  2. Next, fetch deal_discount value, using deal_id which is the entity_id in table deal_offers

I have defined <entities> in my config.xml for these two tables, but not getting success in achieving the desired result using Mage::getModel('module/model') method in my observer.

Please Help

Thanks Thanks


Try using following code snippet, it's an example of inner join operation of two table.

$collection = Mage::getModel('module/modelB')->getCollection()
    ->join('deal_offers', 'main_table.entity_id=deal_offers.deal_id',array('deal_name','deal_discount'));
  • this $collection = Mage::getModel('module/modelB')->getCollection() ->addFieldToFilter('product_id',$product_id); gives me all records. Need to use foreach loop to get a single record.
    – Jordan
    Jul 21 '14 at 7:06
  • That's why there's the second row with JOIN. SQL query language allows to join two tables based on information fetched from either one of them.
    – Pronto
    Jul 21 '14 at 7:45
  • It says Fatal error: Call to a member function getBackend() on a non-object in C:\wamp\www\magento_18\app\code\core\Mage\Eav\Model\Entity\Abstract.php on line 816 when I add addFieldToFilter('product_id',$product_id)
    – Jordan
    Jul 21 '14 at 7:51
  • $collection = Mage::getModel('module/modelB')->getCollection(), gives me array of products and I do not seem to apply filter on this.
    – Jordan
    Jul 21 '14 at 8:44
  • Paste your config.xml please.
    – Sohel Rana
    Jul 21 '14 at 10:28

Try using

$collection = Mage::getModel('module/modelB')->getCollection() ->addFieldToFilter('product_id',$product_id)->getFirstItem();

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.