How to create table in a custom module in magento

12,543

Solution 1

Stuff with mysql4 is outdated for quite a while now. I'd suggest using the following:

Minimum content of config.xml:

<?xml version="1.0"?>
<config>
    <modules>
        <Somnath_Blog>
            <version>1.0.0</version>
        </Somnath_Blog>
    </modules>
    <global>
        <models>
            <blog>
                <class>Somnath_Blog_Model</class>
                <resourceModel>somnath_blog_resource</resourceModel>
            </blog>
            <somnath_blog_resource>
                <class>Somnath_Blog_Model_Resource</class>
                <entities>
                    <blog>
                        <table>blog</table>
                    </blog>
                </entities>
            </somnath_blog_resource>
        </models>
        <resources>
            <somnath_blog_setup>
                <setup>
                    <module>Somnath_Blog</module>
                    <class>Mage_Core_Model_Resource_Setup</class> <!-- optional -->
                </setup>
            </somnath_blog_setup>
        </resources>
    </global>
</config>

Your sql install script in app/code/local/Somnath/Blog/sql/somnath_blog_setup/install-1.0.0.php

/** @var $installer Mage_Core_Model_Resource_Setup */
$installer = $this;

$installer->startSetup();

$table = $installer->getConnection()
    ->newTable($installer->getTable('blog/blog'))
    ->addColumn(
        'blog_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null,
        array(
            'identity' => true,
            'unsigned' => true,
            'nullable' => false,
            'primary'  => true,
        ), 'Unique identifier'
    )
    ->addColumn(
        'title', Varien_Db_Ddl_Table::TYPE_TEXT, 100, array(), 'Blog title'
    )
    ->addColumn(
        'content', Varien_Db_Ddl_Table::TYPE_TEXT, null, array(), 'Blog content'
    )
    ->addColumn(
        'author', Varien_Db_Ddl_Table::TYPE_TEXT, 100, array(), 'Blog author'
    );

if (!$installer->getConnection()->isTableExists($table->getName())) {
    $installer->getConnection()->createTable($table);
}

$installer->endSetup();

And ofcourse you create the basic model / resource models.

Solution 2

Your config.xml has to be in this way.

<?xml version="1.0"?>
<config>
    <modules>
        <[Namespace]_[Module]>
            <version>0.1.0</version>
        </[Namespace]_[Module]>
    </modules>
    <frontend>
        <routers>
            <[module]>
                <use>standard</use>
                <args>
                    <module>[Namespace]_[Module]</module>
                    <frontName>[module]</frontName>
                </args>
            </[module]>
        </routers>
        <layout>
            <updates>
                <[module]>
                    <file>[module].xml</file>
                </[module]>
            </updates>
        </layout>
    </frontend>   
    <global>
        <models>
            <[module]>
                <class>[Namespace]_[Module]_Model</class>
                <resourceModel>[module]_mysql4</resourceModel>
            </[module]>
            <[module]_mysql4>
                <class>[Namespace]_[Module]_Model_Mysql4</class>
                <entities>
                    <[module]>
                        <table>[module]</table>
                    </[module]>
                </entities>
            </[module]_mysql4>
        </models>
        <resources>
            <[module]_setup>
                <setup>
                    <module>[Namespace]_[Module]</module>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </[module]_setup>
            <[module]_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </[module]_write>
            <[module]_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </[module]_read>
        </resources>
        <blocks>
            <[module]>
                <class>[Namespace]_[Module]_Block</class>
            </[module]>
        </blocks>
        <helpers>
            <[module]>
                <class>[Namespace]_[Module]_Helper</class>
            </[module]>
        </helpers>
    </global>
</config>

Check that did you extends resource collection or not. If No, then it has to be in this way.

<?php

class <Namespace>_<Module>_Model_Mysql4_<Module>_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
{
    public function _construct()
    {
        //parent::__construct();
        $this->_init('<module>/<module>');
    }
}

Check this link for more details.

http://www.magentocommerce.com/wiki/5_-_modules_and_development/0_-_module_development_in_magento/custom_module_with_custom_database_table

Share:
12,543
Megha Paul
Author by

Megha Paul

Updated on June 04, 2022

Comments

  • Megha Paul
    Megha Paul about 2 years

    I need to create a new tabel while creating a custom extension in magento. In that case no table is creating and default magento error page is showing. I am giving my code here..Please let me know where did i go wrong. File:/app/code/local/Somnath/Test/sql/test_setup/install-1.6.0.0.php

    $installer = $this;
    
        /* @var $installer Mage_Core_Model_Resource_Setup */
    
    
        $installer->startSetup();
    
    
        $installer->run("
    
        -- DROP TABLE IF EXISTS {$this->getTable('somnath_test')};
        CREATE TABLE {$this->getTable('somnath_test')} (  
          `id` int(11) NOT NULL AUTO_INCREMENT,
          `fname` varchar(100) NOT NULL,
          PRIMARY KEY (`id`)
        ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
    
        ");
    
        $installer->endSetup();
    

    My config.xml file is

    <config>
      <modules>
        <Somnath_Test>
          <version>1.0.0</version>
        </Somnath_Test>
      </modules>
        <frontend>
        <routers>
          <routeurfrontend>
            <use>standard</use>
            <args>
              <module>Somnath_Test</module>
              <frontName>test</frontName>
            </args>
          </routeurfrontend>
        </routers>
        <layout>
          <updates>
            <test>
              <file>test.xml</file>
            </test>
          </updates>
        </layout>
        <strong><events>
       <page_block_html_topmenu_gethtml_before>
        <observers>
           <Somnath_Test>          
               <class>somnath_test/observer</class>
               <method>addToTopmenu</method>
               </Somnath_Test>
           </observers>
      </page_block_html_topmenu_gethtml_before>
    </events>
    </strong>
      </frontend>
      <admin>
         <routers>
             <test>
                <use>admin</use>
                <args>
                   <module>Somnath_Test</module>
                   <frontName>admintest</frontName>
                </args>
             </test>
          </routers>
     </admin>
     <adminhtml>
       <layout>
          <updates>
              <test>
                  <file>test.xml</file>
               </test>
          </updates>
       </layout>
       <menu>
          <test translate="title" module="adminhtml">
             <title>My plugins</title>
             <sort_order>100</sort_order>
             <children>
                 <set_time>
                       <title>Contact Email</title>
                       <action>admintest/adminhtml_index</action>
                  </set_time>
              </children>
           </test>
        </menu>
    </adminhtml>
      <global>
        <blocks>
          <test>
            <class>Somnath_Test_Block</class>
          </test>
        </blocks>
        <models>
          <test>
            <class>Somnath_Test_Model</class>
            <resourceModel>test_mysql4</resourceModel>
          </test>
          <test_mysql4>
            <class>Somnath_Test_Model_Mysql4</class>
            <entities>
              <test>
                <table>somnath_test</table>
              </test>
            </entities>
          </test_mysql4>
        </models>
    
    <resources>
    
            <test_setup>
            <setup>
                    <module>Somnath_Test</module>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </test_setup>
    
            <test_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </test_write>
           <test_read>
              <connection>
                 <use>core_read</use>
              </connection>
           </test_read>
    </resources>
      </global>
    </config>
    
    
    i am trying to build a extension for contact us.My config file is given above and the sql file contents the code above.I have done exactly what is needed to create new table but nothing works.
    
      i can not created table for my custom module. How to create table for my custom module..?Please advice me..