Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7597481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:09:59+00:00 2026-05-30T22:09:59+00:00

3 models mapping 3 tables: Image, Slider and SliderImageAssoc. In this case it’s one-to-many,

  • 0

3 models mapping 3 tables: Image, Slider and SliderImageAssoc. In this case it’s one-to-many, as one Image can be “linked” to many Sliders.

The ZF way, which I’m accustomed to, also proposes 3 models, and, with special internal data stored in them, you can do things like $imageRow->getSliderViaSliderImageAssoc(), resulting the parent row of Slider of that certain Image model.

My question is how exactly does one fetches related models in Magento? I’ve seen methods named setParentFieldName, but I don’t think they were in core. Can you do things like:

foreach ($model->getCollection() as $model) {
  $parentRow       = $model->getParent('some/model/name');
  $dependentRowset = $model->getChildren('some/other/model/name');
}

PS: I don’t necessarily want to use the ZF style of fetching.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T22:10:00+00:00Added an answer on May 30, 2026 at 10:10 pm

    In Magento there is no support for generic mapping of entity table relationships as far as I know. I suggest adding helper methods to the resource model and resource collection to add the joins to the select object if required.

    Here is an examples from the core of the mentioned utility method approach to load additional data on a collection:

    // from Mage_Catalog_Model_Resource_Product_Collection::joinUrlRewrite()
    public function joinUrlRewrite()
    {
        $this->joinTable(
            'core/url_rewrite',
            'entity_id=entity_id',
            array('request_path'),
            '{{table}}.type = ' . Mage_Core_Model_Url_Rewrite::TYPE_PRODUCT,
            'left'
        );
    
        return $this;
    }
    

    If called, the core_url_rewrite table is joined with the product entity table.

    If the joined data needs to be loaded every time, the _getLoadSelect() method can be used for resource models, or _initSelect() for collections.
    Here is an example for that from the cms/page resource model:

    // from Mage_Cms_Model_Resource_Page::_getLoadSelect()
    protected function _getLoadSelect($field, $value, $object)
    {
        $select = parent::_getLoadSelect($field, $value, $object);
    
        if ($object->getStoreId()) {
            $storeIds = array(Mage_Core_Model_App::ADMIN_STORE_ID, (int)$object->getStoreId());
            $select->join(
                array('cms_page_store' => $this->getTable('cms/page_store')),
                $this->getMainTable() . '.page_id = cms_page_store.page_id',
                array())
                ->where('is_active = ?', 1)
                ->where('cms_page_store.store_id IN (?)', $storeIds)
                ->order('cms_page_store.store_id DESC')
                ->limit(1);
        }
    
        return $select;
    }
    

    An example for an _initSelect() join can be found in Mage_CatalogInventory_Model_Resource_Stock_Item_Collection::_initSelect() (I’ll won’t post it here because it is so similar to the _getLoadSelect() example).

    Some modules set joins on collections from “outside” the module, i.e. the stock item collection Mage_CatalogInventory_Model_Resource_Stock_Item::addCatalogInventoryToProductCollection($productCollection). Here the cataloginventory module uses the product collections select object to add some joined data.

    Finally, another approach is to load the required data in the _afterLoad() method doing a separate select (as compared to a join):

    // from Mage_Cms_Model_Resource_Page_Collection::_afterLoad()
    protected function _afterLoad()
    {
        if ($this->_previewFlag) {
            $items = $this->getColumnValues('page_id');
            $connection = $this->getConnection();
            if (count($items)) {
                $select = $connection->select()
                        ->from(array('cps'=>$this->getTable('cms/page_store')))
                        ->where('cps.page_id IN (?)', $items);
    
                if ($result = $connection->fetchPairs($select)) {
                    foreach ($this as $item) {
                        if (!isset($result[$item->getData('page_id')])) {
                            continue;
                        }
                        if ($result[$item->getData('page_id')] == 0) {
                            $stores = Mage::app()->getStores(false, true);
                            $storeId = current($stores)->getId();
                            $storeCode = key($stores);
                        } else {
                            $storeId = $result[$item->getData('page_id')];
                            $storeCode = Mage::app()->getStore($storeId)->getCode();
                        }
                        $item->setData('_first_store_id', $storeId);
                        $item->setData('store_code', $storeCode);
                    }
                }
            }
        }
    
        return parent::_afterLoad();
    }
    

    This can be also done using an *_load_after event for collections or models.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Hibernate's JPA impl to model some tables. I'm having trouble mapping a
Are models available within the modules. Can I access constants stored in a model
Lots out there about changing model names only or mapping new models to existing
I am having an issue with many-to-many mapping using NHibernate. Basically I have 2
I've read many posts about this, the last being a statement saying the behaviour
I am mapping 2 models: User Account class Account has_many :users class User has_one
I have the following mapping: <?xml version='1.0' encoding='utf-8'?> <hibernate-mapping assembly='Core' namespace='Core.Models' xmlns='urn:nhibernate-mapping-2.2'> <class name='Basket'>
My django model looks like this: class Entity(models.Model): name = models.CharField(max_length=40) examples = models.ManyToManyField(Example,
Using C#4.0 Following on from my question How to configure nHibernate for many-many mapping?
Is there an application that can take in my db/schema.rb and my models file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.