Create custom model magento

For displaying the custom table data in the magento admin grid we need to retrieve that data from the table. For that we need to create a custom model.The custom model includes the model , resource and the collection files.

Please create following files inside the model folder (from the file structure folder)and copy and paste the following code. Click here to view the full tutorial which explains how to create magento admin grid.

create_magento_admin_grid_file_structure

Click here to download the full code.

/*
app\code\local\Display\Customgrid\Model\Displaygrid.php
*/
<?php
class Display_Customgrid_Model_Displaygrid extends Mage_Core_Model_Abstract {

    public function __construct()
    {
        parent::__construct();
        $this->_init('customgrid/displaygrid');
    }

}

/*
app\code\local\Display\Customgrid\Model\Resource\Displaygrid.php
*/

<?php
class Display_Customgrid_Model_Resource_Displaygrid extends Mage_Core_Model_Resource_Db_Abstract
{
    /***
     * Initialize resource model
     */
    public function _construct()
    {
        $this->_init('customgrid/displaygrid','contact_id');
    }

}

/*
app\code\local\Display\Customgrid\Model\Resource\Displaygrid\Collection.php
*/

<?php

class Display_Customgrid_Model_Resource_Displaygrid_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract {
    /**
     *
     */
    protected function _construct()
    {
        $this->_init('customgrid/displaygrid');
    }
}

Share this Tutorial