如何在magento中的自定义模块中创建表

时间:2015-01-07 10:05:53

标签: magento module

我需要在magento中创建自定义扩展程序时创建新表格。 在这种情况下,没有表正在创建,并且正在显示默认的magento错误页面。我在这里给我的代码..请让我知道我哪里出错了。 文件:/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();

我的config.xml文件是

<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..

4 个答案:

答案 0 :(得分:2)

使用mysql4的东西已经过时了很长一段时间。我建议使用以下内容:

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>

你的sql安装脚本在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();

当然,您可以创建基本的模型/资源模型。

答案 1 :(得分:1)

您的config.xml必须采用这种方式。

<?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>

检查是否扩展了资源集合。如果不是,则必须采用这种方式。

<?php

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

查看此链接了解更多详情。

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

答案 2 :(得分:0)

请参阅我的教程,其中解释了创建自定义表magento模块的文件结构和代码。 http://www.pearlbells.co.uk/create-custom-table-magento-module/

$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();

答案 3 :(得分:0)

在etc文件夹中创建一个mysql4-install-0.1.0.php。并使用以下脚本在自定义模块中创建表。

<?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();

?>