Today, We want to share with you Magento Create custom Database Table.
In this post we will show you create Magento custom modules, hear for How to create table in a custom module in magento we will give you demo and example for implement.In this post, we will learn about Create a module with custom database table in Magento 2 with an example.
Magento Create custom Database Table with Module
There are the Following The simple About Magento Create custom Database Table with Module Full Information With Example and source code.here simple company = Namespace as well as
/app/code/local/
Block/ controllers/ etc/ Model/ Mysql4/ / sql/ _setup/
Activate Stock_exchange
/app/etc/stock_exchanges/
<[company]_[Stock_exchange]> true local [company]_[Stock_exchange]>
Make Controller
/app/code/local/
loadLayout(); $this->renderLayout(); } }
Make Configuration XML
Magento Create custom Database Table Config files
/app/code/local/
<[company]_[Stock_exchange]> 0.1.0 [company]_[Stock_exchange]><[stock_exchange]> [stock_exchange]> [company]_[Stock_exchange] [stock_exchange] <[stock_exchange]> [stock_exchange].xml [stock_exchange]><[stock_exchange]> [company]_[Stock_exchange]_Model [stock_exchange]_mysql4 [stock_exchange]> <[stock_exchange]_mysql4>[company]_[Stock_exchange]_Model_Mysql4 <[stock_exchange]> [stock_exchange]_mysql4>[stock_exchange]
[stock_exchange]><[stock_exchange]_setup> [company]_[Stock_exchange] [stock_exchange]_setup> <[stock_exchange]_write> [stock_exchange]_write> <[stock_exchange]_read> [stock_exchange]_read> <[stock_exchange]> [company]_[Stock_exchange]_Block [stock_exchange]><[stock_exchange]> [company]_[Stock_exchange]_Helper [stock_exchange]>
Make Helper
/app/code/local/
Make Models
/app/code/local/
/ /Model/ .php __Model_ extends Mage_Core_Model_Abstract { public function _construct() { parent::_construct(); $this->_init(‘ / ’); /app/code/local/
/ /Model/Mysql4/ .php __Model_Mysql4_ extends Mage_Core_Model_Mysql4_Abstract { public function _construct() { $this->_init(‘ / ’, ‘ _id’); } } /app/code/local/
/ /Model/Mysql4/ /Collection.php __Model_Mysql4_ _Collection extends Mage_Core_Model_Mysql4_Collection_Abstract { public function _construct() { //parent::__construct(); $this->_init(‘ / ’); } } Magento Create custom Database Table Tutorials
SQL Setup
/app/code/local/
/ /sql/ _setup/mysql4-install-0.1.0.php simple Magento – Create Custom Module with Custom Database Table Step by step
startSetup(); $installer->run(“ — DROP TABLE IF EXISTS {$this->getTable(‘’)}; CREATE TABLE {$this->getTable(‘ ’)} ( ` _id` int(11) unsigned NOT NULL auto_increment, `title` varchar(255) NOT NULL default ”, `content` text NOT NULL default ”, `status` smallint(6) NOT NULL default ‘0’, `created_time` datetime NULL, `update_time` datetime NULL, PRIMARY KEY (` _id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; “); $installer->endSetup(); Template Design
/app/design/frontend/
/ /layout/ .xml /layout/.xml
<[stock_exchange]_index_index> [stock_exchange]_index_index> /app/design/frontend/
/ /layout/ .xml <[stock_exchange]_index_index> [stock_exchange]_index_index> /app/design/frontend/
/ /template/ / .phtml __(‘Stock_exchange List’) ?>
/’)->load(1); echo $infoStock->get Id(); echo $infoStock->getTitle(); echo $infoStock->getContent(); echo $infoStock->getStatus(); */ /* $i = 0; $datacollect = Mage::getModel(‘ / ’)->getCollection(); $datacollect->setPageSize(5); $datacollect->setCurPage(2); $size = $datacollect->getSize(); $cnt = count($datacollect); foreach ($datacollect as $item) { $i = $i+1; $item->setTitle($i); echo $item->getTitle(); } $datacollect->walk(‘save’); */ /* $dataObj = Mage::getModel(‘ / ’)->load(1); $dataObj->setTitle(‘Welcome To Pakainfo.com’); $dataObj->save(); */ Directory Additions
/app/code/local/
/ / Block/ Adminhtml/ / Edit/ Tab/ controllers/ Adminhtml/ etc/ Helper/ Model/ Mysql4/ / sql/ _setup/Blocks
/app/code/local/
/ /Block/Adminhtml/ .php __Block_Adminhtml_ extends Mage_Adminhtml_Block_Widget_Grid_Container { public function __construct() { $this->_controller = ‘adminhtml_ ’; $this->_blockGroup = ‘ ’; $this->_headerText = Mage::helper(‘ ’)->__(‘Product Manager’); $this->_addButtonLabel = Mage::helper(‘ ’)->__(‘Add Item’); parent::__construct(); } } /app/code/local/
/ /Block/Adminhtml/ /Edit.php __Block_Adminhtml_ _Edit extends Mage_Adminhtml_Block_Widget_Form_Container { public function __construct() { parent::__construct(); $this->_objectId = ‘id’; $this->_blockGroup = ‘ ’; $this->_controller = ‘adminhtml_ ’; $this->_updateButton(‘save’, ‘label’, Mage::helper(‘ ’)->__(‘Save Item’)); $this->_updateButton(‘delete’, ‘label’, Mage::helper(‘ ’)->__(‘Delete Item’)); } public function getHeaderText() { if( Mage::registry(‘ _data’) && Mage::registry(‘ _data’)->getId() ) { return Mage::helper(‘ ’)->__(“Edit Item ‘%s'”, $this->htmlEscape(Mage::registry(‘ _data’)->getTitle())); } else { return Mage::helper(‘ ’)->__(‘Add Item’); } } } /app/code/local/
/ /Block/Adminhtml/ /Grid.php __Block_Adminhtml_ _Grid extends Mage_Adminhtml_Block_Widget_Grid { public function __construct() { parent::__construct(); $this->setId(‘ Grid’); // This is the primary key of the database $this->setDefaultSort(‘ _id’); $this->setDefaultDir(‘ASC’); $this->setSaveParametersInSession(true); } protected function _prepareCollection() { $datacollect = Mage::getModel(‘ / ’)->getCollection(); $this->setCollection($datacollect); return parent::_prepareCollection(); } protected function _prepareColumns() { $this->addColumn(‘ _id’, array( ‘header’ => Mage::helper(‘ ’)->__(‘ID’), ‘align’ =>‘right’, ‘width’ => ’50px’, ‘index’ => ‘ _id’, )); $this->addColumn(‘title’, array( ‘header’ => Mage::helper(‘ ’)->__(‘Title’), ‘align’ =>‘left’, ‘index’ => ‘title’, )); /* $this->addColumn(‘content’, array( ‘header’ => Mage::helper(‘ ’)->__(‘Product Content’), ‘width’ => ‘150px’, ‘index’ => ‘content’, )); */ $this->addColumn(‘created_time’, array( ‘header’ => Mage::helper(‘ ’)->__(‘Creation Time’), ‘align’ => ‘left’, ‘width’ => ‘120px’, ‘type’ => ‘date’, ‘default’ => ‘–‘, ‘index’ => ‘created_time’, )); $this->addColumn(‘update_time’, array( ‘header’ => Mage::helper(‘ ’)->__(‘Update Time’), ‘align’ => ‘left’, ‘width’ => ‘120px’, ‘type’ => ‘date’, ‘default’ => ‘–‘, ‘index’ => ‘update_time’, )); $this->addColumn(‘status’, array( ‘header’ => Mage::helper(‘ ’)->__(‘Status’), ‘align’ => ‘left’, ‘width’ => ’80px’, ‘index’ => ‘status’, ‘type’ => ‘options’, ‘options’ => array( 1 => ‘Active’, 0 => ‘Inactive’, ), )); return parent::_prepareColumns(); } public function getRowUrl($row) { return $this->getUrl(‘*/*/edit’, array(‘id’ => $row->getId())); } } /app/code/local/
/ /Block/Adminhtml/ /Edit/Form.php __Block_Adminhtml_ _Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form { protected function _prepareForm() { $form = new Varien_Data_Form(); $this->setForm($form); $fieldset = $form->addFieldset(‘ _form’, array(‘legend’=>Mage::helper(‘ ’)->__(‘Product information’))); $fieldset->addField(‘title’, ‘text’, array( ‘label’ => Mage::helper(‘ ’)->__(‘Title’), ‘class’ => ‘required-entry’, ‘required’ => true, ‘name’ => ‘title’, )); $fieldset->addField(‘status’, ‘select’, array( ‘label’ => Mage::helper(‘ ’)->__(‘Status’), ‘name’ => ‘status’, ‘values’ => array( array( ‘value’ => 1, ‘label’ => Mage::helper(‘ ’)->__(‘Active’), ), array( ‘value’ => 0, ‘label’ => Mage::helper(‘ ’)->__(‘Inactive’), ), ), )); $fieldset->addField(‘content’, ‘editor’, array( ‘name’ => ‘content’, ‘label’ => Mage::helper(‘ ’)->__(‘Content’), ‘title’ => Mage::helper(‘ ’)->__(‘Content’), ‘style’ => ‘width:98%; height:400px;’, ‘wysiwyg’ => false, ‘required’ => true, )); if ( Mage::getSingleton(‘adminhtml/session’)->get Data() ) { $form->setValues(Mage::getSingleton(‘adminhtml/session’)->get Data()); Mage::getSingleton(‘adminhtml/session’)->set Data(null); } elseif ( Mage::registry(‘ _data’) ) { $form->setValues(Mage::registry(‘ _data’)->getData()); } return parent::_prepareForm(); } } /app/code/local/
/ /Block/Adminhtml/ /Edit/Tabs.php __Block_Adminhtml_ _Edit_Tabs extends Mage_Adminhtml_Block_Widget_Tabs { public function __construct() { parent::__construct(); $this->setId(‘ _tabs’); $this->setDestElementId(‘edit_form’); $this->setTitle(Mage::helper(‘ ’)->__(‘News Information’)); } protected function _beforeToHtml() { $this->addTab(‘form_section’, array( ‘label’ => Mage::helper(‘ ’)->__(‘Product Information’), ‘title’ => Mage::helper(‘ ’)->__(‘Product Information’), ‘content’ => $this->getLayout()->createBlock(‘ /adminhtml_ _edit_tab_form’)->toHtml(), )); return parent::_beforeToHtml(); } } /app/code/local/
/ /Block/Adminhtml/ /Edit/Tab/Form.php __Block_Adminhtml_ _Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form { protected function _prepareForm() { $form = new Varien_Data_Form(); $this->setForm($form); $fieldset = $form->addFieldset(‘ _form’, array(‘legend’=>Mage::helper(‘ ’)->__(‘Product information’))); $fieldset->addField(‘title’, ‘text’, array( ‘label’ => Mage::helper(‘ ’)->__(‘Title’), ‘class’ => ‘required-entry’, ‘required’ => true, ‘name’ => ‘title’, )); $fieldset->addField(‘status’, ‘select’, array( ‘label’ => Mage::helper(‘ ’)->__(‘Status’), ‘name’ => ‘status’, ‘values’ => array( array( ‘value’ => 1, ‘label’ => Mage::helper(‘ ’)->__(‘Active’), ), array( ‘value’ => 0, ‘label’ => Mage::helper(‘ ’)->__(‘Inactive’), ), ), )); $fieldset->addField(‘content’, ‘editor’, array( ‘name’ => ‘content’, ‘label’ => Mage::helper(‘ ’)->__(‘Content’), ‘title’ => Mage::helper(‘ ’)->__(‘Content’), ‘style’ => ‘width:98%; height:400px;’, ‘wysiwyg’ => false, ‘required’ => true, )); if ( Mage::getSingleton(‘adminhtml/session’)->get Data() ) { $form->setValues(Mage::getSingleton(‘adminhtml/session’)->get Data()); Mage::getSingleton(‘adminhtml/session’)->set Data(null); } elseif ( Mage::registry(‘ _data’) ) { $form->setValues(Mage::registry(‘ _data’)->getData()); } return parent::_prepareForm(); } } Controller
simple Magento controller for Magento Create custom Database Table.
/app/code/local/
/ /controllers/Adminhtml/ Controller.php __Adminhtml_ Controller extends Mage_Adminhtml_Controller_Action { protected function _initAction() { $this->loadLayout() ->_setActiveMenu(‘ /items’) ->_addBreadcrumb(Mage::helper(‘adminhtml’)->__(‘Items Manager’), Mage::helper(‘adminhtml’)->__(‘Product Manager’)); return $this; } public function indexAction() { $this->_initAction(); $this->_addContent($this->getLayout()->createBlock(‘ /adminhtml_ ’)); $this->renderLayout(); } public function editAction() { $ Id = $this->getRequest()->getParam(‘id’); $ Model = Mage::getModel(‘ / ’)->load($ Id); if ($ Model->getId() || $ Id == 0) { Mage::register(‘ _data’, $ Model); $this->loadLayout(); $this->_setActiveMenu(‘ /items’); $this->_addBreadcrumb(Mage::helper(‘adminhtml’)->__(‘Product Manager’), Mage::helper(‘adminhtml’)->__(‘Product Manager’)); $this->_addBreadcrumb(Mage::helper(‘adminhtml’)->__(‘Product News’), Mage::helper(‘adminhtml’)->__(‘Product News’)); $this->getLayout()->getBlock(‘head’)->setCanLoadExtJs(true); $this->_addContent($this->getLayout()->createBlock(‘ /adminhtml_ _edit’)) ->_addLeft($this->getLayout()->createBlock(‘ /adminhtml_ _edit_tabs’)); $this->renderLayout(); } else { Mage::getSingleton(‘adminhtml/session’)->addError(Mage::helper(‘ ’)->__(‘Product does not exist’)); $this->_redirect(‘*/*/’); } } public function newAction() { $this->_forward(‘edit’); } public function saveAction() { if ( $this->getRequest()->getPost() ) { try { $postData = $this->getRequest()->getPost(); $ Model = Mage::getModel(‘ / ’); $ Model->setId($this->getRequest()->getParam(‘id’)) ->setTitle($postData[‘title’]) ->setContent($postData[‘content’]) ->setStatus($postData[‘status’]) ->save(); Mage::getSingleton(‘adminhtml/session’)->addSuccess(Mage::helper(‘adminhtml’)->__(‘Product was successfully saved’)); Mage::getSingleton(‘adminhtml/session’)->set Data(false); $this->_redirect(‘*/*/’); return; } catch (Exception $e) { Mage::getSingleton(‘adminhtml/session’)->addError($e->getMessage()); Mage::getSingleton(‘adminhtml/session’)->set Data($this->getRequest()->getPost()); $this->_redirect(‘*/*/edit’, array(‘id’ => $this->getRequest()->getParam(‘id’))); return; } } $this->_redirect(‘*/*/’); } public function deleteAction() { if( $this->getRequest()->getParam(‘id’) > 0 ) { try { $ Model = Mage::getModel(‘ / ’); $ Model->setId($this->getRequest()->getParam(‘id’)) ->delete(); Mage::getSingleton(‘adminhtml/session’)->addSuccess(Mage::helper(‘adminhtml’)->__(‘Product was successfully deleted’)); $this->_redirect(‘*/*/’); } catch (Exception $e) { Mage::getSingleton(‘adminhtml/session’)->addError($e->getMessage()); $this->_redirect(‘*/*/edit’, array(‘id’ => $this->getRequest()->getParam(‘id’))); } } $this->_redirect(‘*/*/’); } public function gridAction() { $this->loadLayout(); $this->getResponse()->setBody( $this->getLayout()->createBlock(‘importedit/adminhtml_ _grid’)->toHtml() ); } } XML Configuration Changes
/app/code/local/
/ /etc/config.xml <[company]_[Stock_exchange]> 0.1.0 [company]_[Stock_exchange]><[stock_exchange]> [stock_exchange]> [company]_[Stock_exchange] [stock_exchange] <[stock_exchange]> [stock_exchange].xml [stock_exchange]><[stock_exchange]> [stock_exchange]> [company]_[Stock_exchange] [stock_exchange] Allow Everything <[stock_exchange]> [Stock_exchange] Stock_exchange 200 [stock_exchange]><[stock_exchange]> [stock_exchange].xml [stock_exchange]><[stock_exchange]> [company]_[Stock_exchange]_Model [stock_exchange]_mysql4 [stock_exchange]> <[stock_exchange]_mysql4>[company]_[Stock_exchange]_Model_Mysql4 <[stock_exchange]> [stock_exchange]_mysql4>[stock_exchange]
[stock_exchange]><[stock_exchange]_setup> [company]_[Stock_exchange] [stock_exchange]_setup> <[stock_exchange]_write> [stock_exchange]_write> <[stock_exchange]_read> [stock_exchange]_read> <[stock_exchange]> [company]_[Stock_exchange]_Block [stock_exchange]><[stock_exchange]> [company]_[Stock_exchange]_Helper [stock_exchange]>XML Layout
/app/design/adminhtml/
/ /layout/ .xml Last step for the Magento Create custom Database with Table full Source code.
<[stock_exchange]_adminhtml_[stock_exchange]_index> [stock_exchange]_adminhtml_[stock_exchange]_index> Angular 6 CRUD Operations Application Tutorials
Read :
Summary
You can also read about AngularJS, ASP.NET, VueJs, PHP.
I hope you get an idea about Magento – Create Custom Module with Custom Database Table.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.