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

问题描述 投票:0回答:4

我需要在 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..
magento module
4个回答
3
投票

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
投票

你的

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


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');

SQL文本;

$installer->run($sql);

$installer->endSetup();

?>

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();
© www.soinside.com 2019 - 2024. All rights reserved.