从magento模块创建一个新表


Create a new table from magento module

我正在尝试让我正在处理的管理模块在数据库中创建一个新表。我在中设置了什么

app/code/local/Foo/BAR/sql/mysql4-install-0.1.0.php

<?php
  $installer = $this;
  $installer->startSetup();
  $installer->run("
    DROP TABLE IF EXISTS {$this->getTable('notes')};
    CREATE TABLE {$this->getTable('notes')} (
      `ppr_id` int(11) NOT NULL AUTO_INCREMENT,
      `notesku` bigint(20) NOT NULL,
      `notestatus`  smallint(16),
      PRIMARY KEY (`notes`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=utf8
  ");
  $installer->endSetup();

这在app/code/local/Foo/BAR/etc/config.xml 中

<?xml version="1.0"?>
<config>
    <modules>
        <Foo_BAR>
            <version>0.1.0</version>
        </Foo_BAR>
    </modules>    
    <global>
            <models>
                <BAR>
                    <class>Foo_BAR_Model</class>
                    <resourceModel>BAR_mysql4</resourceModel>
                </BAR>
                <BAR_myslq4>
                    <class>Foo_BAR_Model_Mysql4</class>
                    <entities>
                        <BAR>
                            <table>notes</table>
                        </BAR>
                    </entities>
                </BAR_myslq4>           
            </models>
            <resources>
                <BAR_setup>
                    <setup>
                        <module>Foo_BAR</module>
                    </setup>
                    <connection>
                        <use>core_setup</use>
                    </connection>
                </BAR_setup>
                <BAR_write>
                    <connection>
                        <use>core_write</use>
                    </connection>
                </BAR_write>
                <BAR_read>
                    <connection>
                        <use>core_read</use>
                    </connection>
                </BAR_read>
            </resources>
    </global>    
    <admin>
        <routers>
            <BAR>
                <use>admin</use>
                <args>
                    <module>Foo_BAR</module>
                    <frontName>bar</frontName>
                </args>
            </BAR>      
        </routers>
    </admin>

      <adminhtml>
        <menu>
            <catalog>
                <children>
                     <BAR_menu translate="title" module="BAR">
                        <title>BAR</title>
                        <children>
                            <list translate="title" module="BAR">
                                <title>Bar</title>
                                <action>bar/index/index</action>
                            </list>                       
                        </children>
                    </BAR_menu>
                </children>
            </catalog>
        </menu>
    </adminhtml>       
</config>

我使用的案例与这个例子相匹配,在这个例子中,我的公司是大写的,模块名称都是大写的。我在想,也许这让我绊倒了?我的理解是,一旦我运行了命中该模块的页面,它就会触发mysql来创建表。这是正确的吗?我还有别的事情要做吗?

我非常感谢在这方面的任何帮助。

如果安装/升级脚本没有运行,以下是需要检查的内容:

  1. Magento正在加载您的模块吗?转到系统>配置>高级>高级,查看您的模块是否出现在"禁用模块输出"列表中。如果没有,Magento根本不会加载您的模块,因此不会运行任何安装脚本。正如Cags在他的评论中所指出的,如果您还没有创建模块,那么您需要app/etc/modules中的一个xml文件来告诉Magento加载您的模块。

  2. 确保您的资源在config.xml文件中的正确位置声明。它们应该在<global>标记内(在您的情况下,这似乎是正确的)。

  3. 请确保您的安装文件位于正确的位置。它们应该在模块内的sql/文件夹中。我认为这是你的问题,这个例子中的设置文件应该是app/code/local/Foo/BAR/sql/BAR_setup/mysql4-install-0.1.0.php

  4. 检查完以上所有内容后,如果您已经为调试设置了IDE(如果您正在做任何严肃的Magento工作,请帮自己一个忙,然后再设置一个断点),请在设置文件中设置断点,并确保它被命中。

  5. 检查数据库中的core_resource表中是否有BAR_setup条目。如果它在那里,Magento已经运行了一次安装脚本,不会再运行了。如果需要再次运行安装脚本,请删除此记录。同样,如果需要重新运行升级脚本,也可以更改版本号(但如果需要,请确保您了解再次运行安装/升级脚本的后果)。

如果其他一切都失败了,请查看Alan Storm的Magento安装脚本调试指南。