Create an magento extension with custom database table
Create an magento extension with custom database table
For,a newcomer magento developer,it is not easy to create an magento extension with custom database.
There are few steps to create an extension.
First of all create module control file Module name as Amit_Custommodule.xml at app/etc/modules/.
Which is enabling and disabling that custom module.First of all define CodePool of extension.
There are two type of codePool one local another community.
Suppose i am using community.Then the code of Amit_Custommodule.xml are:
<?xml version="1.0"?> <config> <modules> <Amit_Custommodule> <codePool>community</codePool> <active>true</active> </Amit_Custommodule> </modules> </config>
Create a config.xml ,where we are declared table,model,blocks,layouts file , controllers,In a word we can say configuration file that extension.
Path Of config.xml is app/code/community/Amit/Custommodule/etc/
As we define codepool is community then it path app/code/community
Here Amit folder is Namespace of module and Custommodule is module name
Here code of config.xml
<?xml version="1.0" ?> <config> <modules> <Amit_Custommodule> <version>1.0.0</version> </Amit_Custommodule> </modules> <global> <models> <custommodule> <class>Amit_Custommodule_Model</class> <resourceModel>custommodule_resource</resourceModel> </custommodule> <custommodule_resource> <class>Amit_Custommodule_Model_Resource</class> <entities> <custommodule> <table>custommodule</table> </custommodule> </entities> </custommodule_resource> </models> <resources> <custommodule_setup> <setup> <module>Amit_Custommodule</module> </setup> <connection> <use>core_setup</use> </connection> </custommodule_setup> <custommodule_read> <connection> <use>core_read</use> </connection> </custommodule_read> <custommodule_write> <connection> <use>core_write</use> </connection> </custommodule_write> </resources> <!-- start of block --> <blocks> <custommodule> <class>Amit_Custommodule_Block</class> </custommodule> </blocks> </global> <!-- start of routers --> <frontend> <routers> <custommodule> <use>standard</use> <args> <module>Amit_Custommodule</module> <frontName>custommodule</frontName> </args> </custommodule> </routers> <layout> <updates> <custommodule> <file>custommodule.xml</file> </custommodule> </updates> </layout> </frontend> </config>
First define the table sql file install-1.0.0.php path app/code/community/Amit/Custommodule/sql/custommodule_setup/
<?php $installer=$this; $installer->startSetup(); $installer->run(" -- DROP TABLE IF EXISTS {$this->getTable('custommodule')}; CREATE TABLE {$this->getTable('custommodule')} ( `custommodule_id` int(11) unsigned NOT NULL auto_increment COMMENT 'Q&A ID', `product_id` int(11) NOT NULL COMMENT 'Product Id' , `customer_name` varchar(255) NOT NULL COMMENT 'Customer Name', `customer_email` varchar(255) NOT NULL COMMENT 'Customer Email', `question` text NOT NULL COMMENT 'Question', `answer` text NOT NULL COMMENT 'Answer', `status` smallint(6) NOT NULL default '0' COMMENT 'Status', `created_time` datetime NULL, `update_time` datetime NULL, PRIMARY KEY (`custommodule_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; "); $installer->endSetup(); ?>
<?php class Amit_Custommodule_IndexController extends Mage_Core_Controller_Front_Action{ public function indexAction(){ } public function askpostAction(){ } }
Now ,we define the model of the module.which fetch ,delete,update data from table
Model file Custommodule.php path is app/code/community/Amit/Custommodule/Model
<?php class Amit_Custommodule_Model_Custommodule extends Mage_Core_Model_Abstract { public function _construct() { $this->_init('custommodule/custommodule'); } }
If we define model then we need define resource class of model Custommodule.php
Resource class my module is Custommodule.php app/code/community/Amit/Custommodule/Model/Resource/
code is
<?php class Amit_Custommodule_Model_Resource_Custommodule extends Mage_Core_Model_Resource_Db_Abstract { /** * Initialize resource model * * @return void */ public function _construct() { $this->_init('custommodule/custommodule', 'custommodule_id'); } }
this two files makes final module Mage::getModel(“custommodule/custommodule”)->load($primaryKeyOfTable);
Now we need to class collection file of that module which is fetch all data of the table
Collection file path is Collection.php app/code/community/Amit/Custommodule/Model/Resource/Custommodule
<?php class Amit_Custommodule_Model_Resource_Custommodule_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract{ protected function _constuct(){ $this->_init('custommodule/custommodule'); } }
This class provide code Mage::getModel(“custommodule/custommodule”)->getCollection();
Date:Apr 15, 2014
Now ,i am start to call frontend routers and for that reasoon i have add routers in config.xml and add the below code after
</custommodule>
</blocks>
</global>…
<frontend> <routers> <custommodule> <use>standard</use> <args> <module>Amit_Custommodule</module> <frontName>custommodule</frontName> </args> </custommodule> </routers> <layout> <updates> <custommodule> <file>custommodule.xml</file> </custommodule> </updates> </layout> </frontend>
Here above code custommodule is routers ,If you want to any url from this modules then url like yousiteurl/custommodule
create an controller,which is generally called as routers
IndexController.php path is app/code/community/Amit/Custommodule/controllers/
<?php class Amit_Custommodule_IndexController extends Mage_Core_Controller_Front_Action{ public function indexAction(){ $this->loadLayout(); $this->renderLayout(); } }
As magento follow this stucture, yoursiteurl/index.php/fontname/controllers/action
then url is yoursiteurl/index.php/custommodule/index/index
I am declaring block class in config.xml and need to add block with in global tag
<blocks>
<custommodule>
<class>Amit_Custommodule_Block</class>
</custommodule>
</blocks>
Next step , i define phtml files in layout xml file… and here layout file for that module is custommodule.xml and it also need to declare in config.xml
code of custommodule.xml path app/design/frontend/yourpackage/your template/layout/
<?xml version="1.0"?> <layout version="0.1.0"> <custommodule_index_index> <reference name="content"> <block type="custommodule/custommodule" name="custommodule" template="custommodule/custommodule.phtml" /> </reference> </custommodule_index_index> </layout>
And phtmls are located in app/design/frontend/yourpackage/your template/template/custommodule
code of custommodule.phtml is
<?php echo get_class($this); $collection=$this->getCollection(); foreach($collection as $each){ echo "<pre>"; print_r($each->getData()); echo "</pre>"; } ?>
As define block type of phtml is “custommodule/custommodule” and the block class is Amit_Custommodule_Block_Custommodule and Custommodule.php located in app\code\community\Amit\Custommodule\Block
<?php class Amit_Custommodule_Block_Custommodule extends Mage_Core_Block_Template{ protected $_Collection=null; public function getCollection(){ if(is_null($this->_Collection)){ $this->_Collection=Mage::getModel('custommodule/custommodule')->getCollection(); } return $this->_Collection; } }