How to I make a Magento module that can CRUD a custom table, inside the admin panel?

How to I make a Magento module that can CRUD a custom table, inside the admin panel?

August 23, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to I make a Magento module that can CRUD a custom table, inside the admin panel?

Creating a Magento Admin Module for CRUD Operations

Introduction

Developing a Magento admin module involves creating a custom extension that interacts with your database within the admin panel interface. This guide will walk you through the process of creating an admin module from scratch to perform Create, Read, Update, and Delete (CRUD) operations on a custom database table in Magento 1.

Prerequisites

  • Basic understanding of XML configuration files (config.xml)
  • Familiarity with Magento architecture and admin panel structure
  • Access to a local or remote server running the latest Magento version (1.7+)

Step 1: Configuration

config.xml File Setup

Create a new config.xml file inside your module’s root directory. Add the following XML content as a starting point:

<?xml version="1.0"?>
<config>
    <modules>
        <Namespace_Modulename>
            <version>1.0.0</version>
        </Namespace_Modulename>
    </modules>

    <global>
        <helpers>
            <modulename>
                <class>Namespace_Modulename_Helper_Data</class>
            </modulename>
        </helpers>

        <models>
            <modulename>
                <class>Namespace_Modulename_Model</class>
                <resourceModel>modulename_mysql4</resourceModel>
            </modulename>
        </models>

        <resources>
            <modulename_setup>
                <setup>
                    <module>Namespace_Modulename</module>
                    <version>1.0.0</version>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </modulename_setup>

            <modulename_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </modulename_write>

            <modulename_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </modulename_read>
        </resources>
    </global>

    <adminhtml>
        <acl>
            <resources>
                <all>
                    <children>
                        <modulename translate="title" module="modulename">
                            <title>Admin Menu Title</title>
                            <sort_order>100</sort_order>
                            <children>
                                <items translate="title" module="modulename">
                                    <title>Items Title</title>
                                    <sort_order>200</sort_order>
                                </items>
                            </children>
                        </modulename>
                    </children>
                </all>
            </resources>
        </acl>
    </adminhtml>

    <install>
        <schedule>
            <modulename_setup>
                <setup:acl></setup:acl>
            </modulename_setup>
        </schedule>
    </install>
</config>

Replace Namespace_Modulename with your module’s namespace and name. Adjust the admin menu title, sort order, and child node titles according to your needs.

Step 2: Database Structure

Creating a Custom Table

Navigate to your local Magento installation’s root directory app/code/core/Mage/Setup/Model/MySQL4/Install/Schema.php. Find the createTables() method and add your custom table creation code within the <installSchema> section. For example:

$installer = $this;
$installer->startSetup();

$table = $installer->getConnection()
    ->newTable($installer->getTable('modulename_entity'))
    ->addColumn('entity_id', Table::TYPE_INTEGER, null, array(
        'identity' => true,
        'unsigned' => true,
        'nullable' => false,
        'primary'  => true,
        ), '')
    ->addColumn('name', Table::TYPE_VARCHAR, 255, array(
        ), 'Name')
    ->addColumn('description', Table::TYPE_TEXT, null, array(
        ), 'Description');

$installer->getConnection()->createTable($table);

$installer->endSetup();

This example creates a simple table named modulename_entity with three columns: entity_id, name, and description.

Step 3: Model and Resource

Creating the Module Model

Create a new file inside your module’s root directory in the app/code/local/Namespace/Modulename/Model/ folder. Name it Entity.php. Add the following code:

class Namespace_Modulename_Model_Entity extends Mage_Core_Model_Abstract
{
    protected function _construct()
    {
        $this->_init('modulename/entity');
    }
}

This model corresponds to your custom table and is responsible for interacting with it.

Creating the Resource Model

Create a new file inside your module’s root directory in the app/code/local/Namespace/Modulename/Model/Mysql4/ folder. Name it Entity.php. Add the following code:

class Namespace_Modulename_Model_Mysql4_Entity extends Mage_Core_Model_Mysql4_Abstract
{
    public function _construct()
    {
        $this->_init('modulename/entity', 'entity_id');
    }
}

This resource model handles data persistence for your custom table.

Step 4: Controller and Block

Creating the Admin Grid

Create a new file inside your module’s root directory in the app/code/local/Namespace/Modulename/controllers/Adminhtml/ folder. Name it EntityController.php. Add the following code as a starting point:

class Namespace_Modulename_Adminhtml_EntityController extends Mage_Adminhtml_Controller_Action
{
    public function indexAction()
    {
        $this->_title($this->__('Items'));

        $this->loadLayout();
        $this->_setActiveMenu('modulename/items');

        $this->_addContent($this->getLayout()->createBlock('modulename/adminhtml_entity'))
            ->_addLeft($this->getLayout()->createBlock('modulename/adminhtml_entity_toolbar'));

        $this->renderLayout();
    }

    public function editAction()
    {
        // Your edit action code here
    }

    public function saveAction()
    {
        // Your save action code here
    }

    public function deleteAction()
    {
        // Your delete action code here
    }
}

Creating the Admin Grid Block

Create a new file inside your module’s root directory in the app/code/local/Namespace/Modulename/Block/Adminhtml/ folder. Name it Entity.php. Add the following code:

class Namespace_Modulename_Block_Adminhtml_Entity extends Mage_Adminhtml_Block_Widget_Grid
{
    public function __construct()
    {
        parent::__construct();
        $this->setId('modulename_entity');
        $this->setDefaultSort('entity_id');
        $this->setDefaultDir('DESC');
        $this->setSaveParametersInSession(true);
    }

    protected function _prepareCollection()
    {
        $collection = Mage::getModel('modulename/entity')->getCollection();
        $this->setCollection($collection);
        return parent::_prepareCollection();
    }

    protected function _prepareColumns()
    {
        $this->addColumn('entity_id', array(
            'header' => $this->__('ID'),
            'align'  => 'right',
            'width'  => '50px',
            'index'  => 'entity_id',
        ));

        // Add more columns as needed

        return parent::_prepareColumns();
    }
}

Step 5: Routing and Layout Handling

Updating the config.xml

Add the following XML within the <adminhtml> section of your config.xml file:

<router>
    <modulename_entity>
        <args>
            <module>Namespace_Modulename</module>
        </args>
        <routes>
            <items>
                <uses>adminhtml</uses>
                <acl>modulename/items</acl>
            </items>
        </routes>
    </modulename_entity>
</router>

Adding Layout Handles

Create a new file inside your module’s root directory in the app/code/local/Namespace/Modulename/layout/ folder. Name it adminhtml.xml. Add the following XML content:

<layout version="1.0">
    <default>
        <reference name="content">
            <block type="modulename/adminhtml_entity" before="-" template="modulename/entity.phtml"/>
        </reference>

        <reference name="head">
            <action method="addJs"><file>modulename/items.js</file></action>
            <action method="addCss"><file>modulename/items.css</file></action>
        </reference>
    </default>
</layout>

This XML file defines the layout handles for your admin module.

Conclusion

Creating a Magento admin module for CRUD operations involves configuring the XML files, setting up the database structure, creating models and resource models, developing the controller and block, updating routing and layout handling. The provided code examples serve as a foundation to help you build upon them according to your specific requirements.

Remember to flush your Magento cache after making any changes to ensure that the new module is loaded correctly. You can do this by navigating to System > Cache Management in the admin panel and selecting Flush Cache Storage.

By following this comprehensive guide, you should now have a solid understanding of how to develop an admin module for CRUD operations on a custom table within the Magento 1 admin panel.

For more detailed information and advanced tutorials on Magento development, visit Laravel Company, where we provide expert guidance and resources.

Magento 1 Database Extensions Admin

Our network