Skip to content

Commit 435a06d

Browse files
committed
how to join three tables on every time when model load
1 parent 66c94b0 commit 435a06d

File tree

2 files changed

+148
-0
lines changed

2 files changed

+148
-0
lines changed

54946/doc.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
In this case,you need make one table primary table and using join function[left join,Inner join] on Resource Model and Resource Model Collection class relation rest of two classes.
2+
Add Tables into Model Collection :
3+
4+
You need to first work on collection class.Here,you need do add function _afterLoad() on collection class,w using this function you can mapping relation between there table.
5+
6+
On Resource Class : Mynamespace_Mymodule_Model_Resource_Mymodel_Collection
7+
8+
protected function _afterLoad()
9+
{
10+
$select= $this->getSelect();
11+
$select->joinLeft(
12+
array('SecondTable'=>$this->getTable('yourmodel/secondtable')),
13+
''Maintable.key=SecondTable.key',
14+
array('*')
15+
);
16+
17+
$select->joinLeft(
18+
array('3rdTable'=>$this->getTable('yourmodel/thirdatble')),
19+
'Maintable.key=3rdTable.key',
20+
array('*')
21+
);
22+
return parent::_afterLoad();
23+
}
24+
25+
afterLoad() trigger a perform a operations after collection load.
26+
Add Three tables into model
27+
28+
On resource class you need add multiple table using function _getLoadSelect() on Mynamespace_Mymodule_Model_Resource_Mymodel
29+
30+
protected function _getLoadSelect($field, $value, $object)
31+
{
32+
$select = parent::_getLoadSelect($field, $value, $object);
33+
34+
$select->join(
35+
array('SecondTable' => $this->getTable('yourmodel/secondtable')),
36+
'Maintable.key=SecondTable.key',
37+
array());
38+
39+
$select->join(
40+
array('3rdTable' => $this->getTable('yourmodel/thirdatble')),
41+
'Maintable.key=3rdTable.key',
42+
array());
43+
44+
return $select;
45+
}
46+
47+
If resource collection is not work then try this code at collection.php
48+
49+
Create two individual function.which is include rest of function to collection.
50+
51+
public function addSecondTable(){
52+
53+
$select= $this->getSelect();
54+
$select->joinLeft(
55+
array('SecondTable'=>$this->getTable('yourmodel/SecondTable')),
56+
'Maintable.key=SecondTable.key',
57+
array('*')
58+
return $this;
59+
}
60+
61+
public function addThirdTable(){
62+
63+
$this->getSelect()->joinLeft(
64+
array('3rdTable'=>$this->getTable('yourmodel/Thirdatble')),
65+
'Maintable.key=3rdTable.key',
66+
array('*'));
67+
return $this;
68+
}
69+
70+
You can use:
71+
72+
$collection = Mage::getResourceModel('mynamespace_mymodule/mymodule_collection');
73+
$collection->addSecondTable()->addThirdTable();
74+

54946/doc.md~

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
In this case,you need make one table primary table and using join function[left join,Inner join] on Resource Model and Resource Model Collection class relation rest of two classes.
2+
Add Tables into Model Collection :
3+
4+
You need to first work on collection class.Here,you need do add function _afterLoad() on collection class,w using this function you can mapping relation between there table.
5+
6+
On Resource Class : Mynamespace_Mymodule_Model_Resource_Mymodel_Collection
7+
8+
protected function _afterLoad()
9+
{
10+
$select= $this->getSelect();
11+
$select->joinLeft(
12+
array('SecondTable'=>$this->getTable('yourmodel/secondtable')),
13+
''Maintable.key=SecondTable.key',
14+
array('*')
15+
);
16+
17+
$select->joinLeft(
18+
array('3rdTable'=>$this->getTable('yourmodel/thirdatble')),
19+
'Maintable.key=3rdTable.key',
20+
array('*')
21+
);
22+
return parent::_afterLoad();
23+
}
24+
25+
afterLoad() trigger a perform a operations after collection load.
26+
Add Three tables into model
27+
28+
On resource class you need add multiple table using function _getLoadSelect() on Mynamespace_Mymodule_Model_Resource_Mymodel
29+
30+
protected function _getLoadSelect($field, $value, $object)
31+
{
32+
$select = parent::_getLoadSelect($field, $value, $object);
33+
34+
$select->join(
35+
array('SecondTable' => $this->getTable('yourmodel/secondtable')),
36+
'Maintable.key=SecondTable.key',
37+
array());
38+
39+
$select->join(
40+
array('3rdTable' => $this->getTable('yourmodel/thirdatble')),
41+
'Maintable.key=3rdTable.key',
42+
array());
43+
44+
return $select;
45+
}
46+
47+
If resource collection is not work then try this code at collection.php
48+
49+
Create two individual function.which is include rest of function to collection.
50+
51+
public function addSecondTable(){
52+
53+
$select= $this->getSelect();
54+
$select->joinLeft(
55+
array('SecondTable'=>$this->getTable('yourmodel/SecondTable')),
56+
'Maintable.key=SecondTable.key',
57+
array('*')
58+
return $this;
59+
}
60+
61+
public function addThirdTable(){
62+
63+
$this->getSelect()->joinLeft(
64+
array('3rdTable'=>$this->getTable('yourmodel/Thirdatble')),
65+
'Maintable.key=3rdTable.key',
66+
array('*'));
67+
return $this;
68+
}
69+
70+
You can use:
71+
72+
$collection = Mage::getResourceModel('mynamespace_mymodule/mymodule_collection');
73+
$collection->addSecondTable()->addThirdTable();
74+

0 commit comments

Comments
 (0)