{"id":9759,"date":"2013-03-07T17:12:40","date_gmt":"2013-03-07T11:42:40","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=9759"},"modified":"2016-11-30T12:38:09","modified_gmt":"2016-11-30T07:08:09","slug":"multiple-db-tables-in-single-custom-module-in-magento","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/multiple-db-tables-in-single-custom-module-in-magento\/","title":{"rendered":"Multiple db tables in single custom module in Magento"},"content":{"rendered":"<p>Creating a module which interact with a database table is quite simple. Most of the <a title=\"custom cms development services\" href=\"http:\/\/www.tothenew.com\/wcm\/web-content-management-services\">developers use magento<\/a> module creator to create such a module. However, what if you want a module with multiple database tables. Following is the example of module with two database tables.<\/p>\n<p>Step 1. Create setup file of your custom module with following queries.<\/p>\n<p>[php]<br \/>\nCREATE TABLE `test` (<br \/>\n`test_id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,<br \/>\n`name` VARCHAR( 25 ) NOT NULL<br \/>\n) ENGINE = MYISAM <\/p>\n<p>CREATE TABLE `test2` (<br \/>\n`test2_id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,<br \/>\n`name` VARCHAR( 25 ) NOT NULL<br \/>\n) ENGINE = MYISAM<br \/>\n[\/php]<\/p>\n<p>Step 2. Create pool file to register your module under app\/etc\/modules\/Mypackage_Mymodule.xml<\/p>\n<p>[php]<br \/>\n&lt;?xml version=&quot;1.0&quot;?&gt;<br \/>\n&lt;config&gt;<br \/>\n    &lt;modules&gt;<br \/>\n        &lt;Mypackage_Mymodule&gt;<br \/>\n            &lt;active&gt;true&lt;\/active&gt;<br \/>\n            &lt;codePool&gt;local&lt;\/codePool&gt;<br \/>\n        &lt;\/Mypackage_Mymodule&gt;<br \/>\n    &lt;\/modules&gt;<br \/>\n&lt;\/config&gt;<br \/>\n[\/php]<\/p>\n<p>Step 3. Your module configuration file should looks like following<\/p>\n<p>app\/code\/local\/ Mypackage\/Mymodule\/etc\/config.xml<\/p>\n<p>[php]<br \/>\n&lt;?xml version=&quot;1.0&quot;?&gt;<br \/>\n&lt;config&gt;<br \/>\n    &lt;modules&gt;<br \/>\n        &lt;Mypackage_Mymodule&gt;<br \/>\n            &lt;version&gt;0.1.0&lt;\/version&gt;<br \/>\n        &lt;\/Mypackage_Mymodule&gt;<br \/>\n    &lt;\/modules&gt;<br \/>\n    &lt;global&gt;<br \/>\n        &lt;models&gt;<br \/>\n\t\t\t&lt;Mymodule&gt;<br \/>\n\t\t\t\t&lt;class&gt;Mypackage_Mymodule_Model&lt;\/class&gt;<br \/>\n\t\t\t\t&lt;resourceModel&gt;mymodule_mysql4&lt;\/resourceModel&gt;<br \/>\n\t\t\t&lt;\/mymodule&gt;<\/p>\n<p>\t\t\t&lt;!&#8211; model vs db table relation &#8211;&gt;<br \/>\n            &lt;mymodule_mysql4&gt;<br \/>\n                &lt;class&gt;Mypackage_Mymodule_Model_Mysql4&lt;\/class&gt;<br \/>\n\t\t\t\t&lt;!&#8211; db table with name test &#8211;&gt;<br \/>\n                &lt;entities&gt;<br \/>\n                    &lt;test&gt;<br \/>\n                        &lt;table&gt;test&lt;\/table&gt;<br \/>\n                    &lt;\/test&gt;<br \/>\n\t\t     &lt;test2&gt;<br \/>\n                        &lt;table&gt;test2&lt;\/table&gt;<br \/>\n                    &lt;\/test2&gt;<\/p>\n<p>                &lt;\/entities&gt;<br \/>\n            &lt;\/mymodule_mysql4&gt;<br \/>\n        &lt;\/models&gt;<br \/>\n\t\t&lt;resources&gt;<br \/>\n\t\t\t&lt;mymodule_write&gt;<br \/>\n\t\t\t\t&lt;connection&gt;<br \/>\n\t\t\t\t\t&lt;use&gt;core_write&lt;\/use&gt;<br \/>\n\t\t\t\t&lt;\/connection&gt;<br \/>\n\t\t\t&lt;\/mymodule_write&gt;<br \/>\n\t\t\t&lt;mymodule_read&gt;<br \/>\n\t\t\t\t&lt;connection&gt;<br \/>\n\t\t\t\t\t&lt;use&gt;core_read&lt;\/use&gt;<br \/>\n\t\t\t\t&lt;\/connection&gt;<br \/>\n\t\t\t&lt;\/mymodule_read&gt;<br \/>\n\t\t&lt;\/resources&gt;<br \/>\n    &lt;\/global&gt;<br \/>\n&lt;\/config&gt;<br \/>\n[\/php]<\/p>\n<p>Step 4. Now create models Test.php and Test2.php. Here we configure these model with the handler of table test and test2.<\/p>\n<p>\/app\/code\/local\/Mypackage\/Mymodule\/Model\/Test.php<\/p>\n<p>[php]<br \/>\n&lt;?php<\/p>\n<p>class Mypackage_ Mymodule_Model_Test extends Mage_Core_Model_Abstract<br \/>\n{<\/p>\n<p>\tpublic function _construct()<br \/>\n    {<br \/>\n        parent::_construct();<br \/>\n        $this-&gt;_init(&#8216;mymodule\/test&#8217;);<br \/>\n    }<br \/>\n}<br \/>\n[\/php]<\/p>\n<p>\/app\/code\/local\/Mypackage\/Mymodule\/Model\/Test2.php<\/p>\n<p>[php]<br \/>\n&lt;?php<\/p>\n<p>class Mypackage_Mymodule_Model_Test2 extends Mage_Core_Model_Abstract<br \/>\n{<\/p>\n<p>\tpublic function _construct()<br \/>\n    {<br \/>\n        parent::_construct();<br \/>\n        $this-&gt;_init(&#8216;mymodule\/test2&#8217;);<br \/>\n    }<br \/>\n}<br \/>\n[\/php]<\/p>\n<p>Step 5. Now create the resource models for model test and test2. In these files we also set the primary key of both the tables test and test2.<\/p>\n<p>\/app\/code\/local\/Mypackage\/Mmodule\/Model\/Mysql4\/Test.php.<\/p>\n<p>[php]<br \/>\n&lt;?php<\/p>\n<p>class Mypackage_Mymodule_Model_Mysql4_Test extends Mage_Core_Model_Mysql4_Abstract<br \/>\n{<br \/>\n    public function _construct()<br \/>\n    {<br \/>\n        $this-&gt;_init(&#8216;mymodule\/test&#8217;, &#8216;test_id&#8217;);<br \/>\n    }<br \/>\n}<br \/>\n[\/php]<\/p>\n<p>\/app\/code\/local\/Mypackage\/Mmodule\/Model\/Mysql4\/Test2.php.<\/p>\n<p>[php]<br \/>\n&lt;?php<\/p>\n<p>class Mypackage_Mymodule_Model_Mysql4_Test2 extends Mage_Core_Model_Mysql4_Abstract<br \/>\n{<br \/>\n    public function _construct()<br \/>\n    {<br \/>\n        $this-&gt;_init(&#8216;mymodule\/test2&#8217;, &#8216;test2_id&#8217;);<br \/>\n    }<br \/>\n}<br \/>\n[\/php]<\/p>\n<p>Step 6. Create a collection classes so that we can retrieve data from table test and test2.<\/p>\n<p>\/local\/Mypackage\/Mymodule\/Model\/Mysql4\/Test\/Collection.php<\/p>\n<p>[php]<br \/>\n&lt;?php<\/p>\n<p>class Mypackage_Mymodule_Model_Mysql4_Test_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract<br \/>\n{<br \/>\n    public function _construct()<br \/>\n    {<br \/>\n        parent::_construct();<br \/>\n        $this-&gt;_init(&#8216;mymodule\/test&#8217;);<br \/>\n    }<br \/>\n}<br \/>\n[\/php]<\/p>\n<p>\/local\/Mypackage\/Mymodule\/Model\/Mysql4\/Test2\/Collection.php<\/p>\n<p>[php]<br \/>\n&lt;?php<\/p>\n<p>class Mypackage_Mymodule_Model_Mysql4_Test2_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract<br \/>\n{<br \/>\n    public function _construct()<br \/>\n    {<br \/>\n        parent::_construct();<br \/>\n        $this-&gt;_init(&#8216;mymodule\/test2&#8217;);<br \/>\n    }<br \/>\n}<br \/>\n[\/php]<\/p>\n<p>So, now you have a custom module with two tables. Your module can interact with these tables with their models and respective collections as follows :<\/p>\n<p>[php]<br \/>\n$testModel = Mage::getModel(&#8216;mymodule\/test&#8217;)<br \/>\n\t-&gt;setName(&quot;abcd&quot;)<br \/>\n\t-&gt;save();<\/p>\n<p>$test2Model = Mage::getModel(&#8216;mymodule\/test2&#8217;)<br \/>\n\t-&gt;setName(&quot;abcd&quot;)<br \/>\n\t-&gt;save();<br \/>\n[\/php]<\/p>\n<p>That&#8217;s it. Hope this will help you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Creating a module which interact with a database table is quite simple. Most of the developers use magento module creator to create such a module. However, what if you want a module with multiple database tables. Following is the example of module with two database tables. Step 1. Create setup file of your custom module [&hellip;]<\/p>\n","protected":false},"author":62,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":4},"categories":[1],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/9759"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/62"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=9759"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/9759\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=9759"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=9759"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=9759"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}