How to tell which table should take in prepare collection method?
-
12-12-2019 - |
Question
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!
Solution
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
Mage::getModel('custommodule/custommoduletwo')
// as entities is custommoduletwoMage::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