1

From this Change table name for admin grid collections thread, one doubt has come into being.

If we have been using 2 table in our custom module, how to tell which table should take in prepare collection method?

Eagarly awaiting for your response!

4
  • are you want to used multiple tables for a model? not module
    – Amit Bera
    Jan 9, 2015 at 10:51
  • for a module...
    – DRAJI
    Jan 9, 2015 at 10:53
  • on one model or multiple model ,like Mage_Catalog have Mage::getModel('catalog/product') and Mage::getModel('catalog/category') ?
    – Amit Bera
    Jan 9, 2015 at 10:54
  • If you explain both type, It will be fine. I don't have any basic idea too in this area..
    – DRAJI
    Jan 9, 2015 at 10:57

1 Answer 1

2

Multiple Model for a module with One Table for each Model

Creation of Multiple Model for a model is easy.you need to define multiple entities to your Module Model resourceModel and it corresponding entity entity_id Table.

Example:

        <custommodule_resource>
            <class>Amit_Custommodule_Model_Resource</class>
            <entities>
                <custommoduleone>
                    <table>custommoduleTestOne</table>
                </custommoduleone>
        <custommoduletwo>
                    <table>custommoduleTestTwo</table>
        </custommoduletwo>
            </entities>
        </custommodule_resource>

Full model code for Module:

<models>
    <custommodule>
    <class>Amit_Custommodule_Model</class>
    <resourceModel>custommodule_resource</resourceModel>
    </custommodule>
    <custommodule_resource>
    <class>Amit_Custommodule_Model_Resource</class>
    <entities>
        <custommoduleone>
        <table>custommoduleTestOne</table>
        </custommoduleone>
        <custommoduletwo>
        <table>custommoduleTestTwo</table>
        </custommoduletwo>
    </entities>
    </custommodule_resource>
</models>

This model give you two model for a module

  1. Mage::getModel('custommodule/custommoduletwo') // as entities is custommoduletwo
  2. Mage::getModel('custommodule/custommoduleone')

Process of define class

According to magento for each Model required need to create Corresponding Model,Resource,Collection class

Model class for model Mage::getModel('custommodule/custommoduleone')

<?php
class Amit_Custommodule_Model_Custommoduleone extends Mage_Core_Model_Abstract
{
    public function _construct()
    {
        $this->_init('custommodule/custommoduleone');
    }

}

Resource class

app/code/community/Amit/Custommodule/Model/Resource/Custommoduleone.php - See more at:

<?php
class Amit_Custommodule_Model_Resource_Custommoduleone extends Mage_Core_Model_Resource_Db_Abstract
{
    /**
     * Initialize resource model
     *
     * @return void
     */
    public function _construct()
    {
        $this->_init('custommodule/custommoduleone', 'table_Primarykey');
    }
}

Collection class:

Collection file path is Collection.php app/code/community/Amit/Custommodule/Model/Resource/Custommoduleone

<?php
class Amit_Custommodule_Model_Resource_Custommoduleone_Collection
extends Mage_Core_Model_Resource_Db_Collection_Abstract{
    protected function _constuct(){
        $this->_init('custommodule/custommoduleone');    
    }
}

Second model:

Model class

<?php
class Amit_Custommodule_Model_CustommoduleTwo extends Mage_Core_Model_Abstract
{
    public function _construct()
    {
        $this->_init('custommodule/custommoduletwo');
    }

}

Resource class

app/code/community/Amit/Custommodule/Model/Resource/Custommoduletwo.php

<?php
class Amit_Custommodule_Model_Resource_Custommoduletwo extends Mage_Core_Model_Resource_Db_Abstract
{
    /**
     * Initialize resource model
     *
     * @return void
     */
    public function _construct()
    {
        $this->_init('custommodule/custommoduletwo', 'table_Primarykey');
    }
}

Collection class:

Collection file path is Collection.php app/code/community/Amit/Custommodule/Model/Resource/Custommoduletwo

<?php
class Amit_Custommodule_Model_Resource_Custommoduletwo_Collection
extends Mage_Core_Model_Resource_Db_Collection_Abstract{
    protected function _constuct(){
        $this->_init('custommodule/custommoduletwo');    
    }
}

NOte:I will provide multiple table for one model shorly

2
  • Thank you so much. Now I got clear idea about how to use multiple table with multiple model(Each table for each model). Really nice and clear explanation.
    – DRAJI
    Jan 9, 2015 at 11:50
  • enthusiastically staying here for your answer of multiple table for one model....
    – DRAJI
    Jan 9, 2015 at 11:52

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.