2

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..
3
  • Did u get any error message ? And your config.xml file seems like incomplete. Try to post full file of config.xml Commented Jan 7, 2015 at 10:21
  • i updated the xml file.please check Commented Jan 7, 2015 at 11:05
  • not working file Commented Apr 29, 2020 at 14:43

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

Create a mysql4-install-0.1.0.php in etc folder. And use the below script to create table in custom module.

<?php
$installer = $this;
$installer->startSetup();
$sql=<<<SQLTEXT
create table tablename(tablename_id int not null auto_increment, name varchar(100), primary key(tablename_id));
    insert into tablename values(1,'tablename1');
    insert into tablename values(2,'tablename2');

SQLTEXT;

$installer->run($sql);

$installer->endSetup();

?>

Comments

0
$installer = $this;

$installer->startSetup();

$installer->run("

DROP TABLE IF EXISTS `{$this->getTable('customer_form_table')}`;
CREATE TABLE `{$this->getTable('customer_form_table')}` (
`log_id` int(11) unsigned NOT NULL auto_increment,
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
`admin_id` int(11) unsigned NOT NULL default '0',
`customer_id` int(11) unsigned NOT NULL default '0',
`terms_old` int(11) unsigned NOT NULL default '0',
`terms_new` int(11) unsigned NOT NULL default '0',
`limit_old` DECIMAL(10,5) NOT NULL default '0',
`limit_new` DECIMAL(10,5) NOT NULL default '0',
`available_old` DECIMAL(10,5) unsigned NOT NULL default '0',
`available_new` DECIMAL(10,5) unsigned NOT NULL default '0',
 PRIMARY KEY (`log_id`),
 INDEX (customer_id),
 INDEX (admin_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 ");

 $installer->endSetup();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.