Create Custom table in magento:
Create and use your custom table in magento is a simple way to configure. Follow below steps to create or use your custom table collection in magento. This creation of table is, as per magento flow it will helpfull to fetch the collection. Like we can use, getModel(“YourMoudle”)->Yourfunction() and fetch the collection from our custom table. Step 1: Create your custom table structure in sql file from Mymodule/sql/mymodule_setup. Sample Code: (for first time)
$installer = $this; $installer->startSetup(); $installer->run(“ DROP TABLE IF EXISTS `testing`; CREATE TABLE IF NOT EXISTS `testing` ( `entity_id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, `test_name` VARCHAR(255) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
example file name is mysql4-install-0.1.0.php (first time)
mysql4-upgrade-0.1.0-0.1.2 (2nd time)
mysql4-upgrade-0.1.2-0.2.1 (3rd time), each upgrade file extention of file will be increase like 0.1.0 to 0.1.2, etc…
Sample Code for (Upgrade file):
<?php $installer = $this; $installer->startSetup(); $tableCustomSetup = $installer->getTable(‘YOURTABLENAME’); $installer->getConnection()->addColumn($tableCustomSetup, ‘column1’, “INT(11) NOT NULL”); $installer->getConnection()->addColumn($tableCustomSetup, ‘column2’, “INT(11) NOT NULL”); $installer->endSetup();
Step 2:
Configure, created mysql file into config.xml file
Ex: <?xml version=”1.0″?> <config> <modules> <Jute_Mymodule> <version>0.1.0</version> </Jute_Mymodule> </modules> …… ……
Step 3:
Once above steps is done, then refresh the page. You will get the “testing” table into your DB.
Step 4:
Now you can access your table “testing” with Mage::getSingleton(‘core/resource’) way. But, instead of this will access table via magento way like Mage::getModel(‘mymodule/mymodelfile’).For that we have to create some model file and need to map your table into config.xml file.
Step 5:
Map your table into config.xml <models> <mymodule> <class>Jute_Mymodule_Model</class> <resourceModel>mymodule_mysql4</resourceModel> </mymodule> <mymodule_mysql4> <class>Jute_Mymodule_Model_Mysql4</class> <entities> <testingfilename><table>testing</table></testingfilename> </entities> </mymodule_mysql4> </models>
Step 6:
Create file name called testingfilename.php from your Model folder. Model/Testingfilename.php
Ex:
class Jute_Mymodule_Model_Testingfilename extends Mage_Core_Model_Abstract
{ public function __construct() { $this->_init(‘mymodule/testingfilename’); }
}
Step 7:
Mention your Increamented ID for fetching a collection when you load your model. Model/Mysql4/Testingfilename.php
class Jute_Mymodule_Model_Mysql4_Testingfilename extends Mage_Core_Model_Mysql4_Abstract
{ protected function _construct() { $this->_init(“mymodule/testingfilename”,”id”); // here id is my increament id from testing table }
}
Step 8:
Now your module is ready for writing or fetching a collection.Collection file you have mention like below
Model/Mysql4/Testingfilename/Collection.php
class Jute_Mymodule_Model_Mysql4_Testingfilename_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
{ protected function _construct() { $this->_init(‘mymodule/testingfilename’); }
}
Step 9:
Yes, now your module is ready to show your collection from your cutom table by Primary Key
Step 10:
echo getModel(‘mymodule/testingfilename’)->load() // by id. Yes finally you got the custom table values by Primary Key