magento在现有表中添加列

Mes*_*aza 2 php magento

我是Magento的新手.我想在添加列newsletter_subscriber的表,所以我做了一个新的文件mysql4-upgrade-1.6.0.0-1.6.0.1.phpapp/code/core/mage/newsletter_setup/

<?php
$installer = $this;
$installer->startSetup();
$installer->getConnection()->addColumn(
    $this->getTable('newsletter/subscriber'), //table name 
    'groupid',                                //column name
    'varchar(100) NOT NULL'                   //datatype definition
);

$installer->endSetup();

?>
Run Code Online (Sandbox Code Playgroud)

我更新了配置文件:

<modules>
    <Mage_Newsletter>
        <version>1.6.0.0</version> 
    </Mage_Newsletter>
</modules>
Run Code Online (Sandbox Code Playgroud)

它不起作用,请指导我做错了什么

小智 5

建议不要添加/修改或更改任何核心文件.最好是制作一个新模块来添加额外的列.

您必须在app/code/local/your/module/sql/your_module_setup/upgrade-0.1.2-0.1.3.php文件中提及模块升级的正确版本.(这意味着您将模块版本从0.1.2升级到0.1.3).如果你没有使用升级脚本,你记得<resources>在模块中定义,config.xml安装脚本名称是mysql4-install-0.1.0.php

下面是Mysql安装脚本文件 - upgrade-0.1.2-0.1.3.php

    <?php
        ini_set('display_errors', '1');

        $installer = $this;
        $installer->startSetup();
        $installer->getConnection()
                 ->addColumn(
                  $installer->getTable('newsletter/subscriber'), //Get the newsletter Table
                  'your_field_name', //New Field Name
             array(
               'type'      => Varien_Db_Ddl_Table::TYPE_TEXT, //Field Type like TYPE_INTEGER ...
               'nullable'  => true,
               'length'    => 255,
               'default'   => 'Some thing default value',
               'comment'   => 'Your field comment'
            )
        );             
        $installer->endSetup();
        ?>
Run Code Online (Sandbox Code Playgroud)

然后更改app/code/local/your/module/etc/config.xml版本

<config>
    <modules>
        <NameSpace_ModuleName>
            <version>0.1.3</version> <!-- if upgrade script version is 0.1.3 -->
        </NameSpace_ModuleName>
    </modules>
   <global>
     <resources>
        <NameSpace_ModuleName_setup>
            <setup>
                <module>NameSpace_ModuleName</module>
                <class>Mage_Catalog_Model_Resource_Setup</class>
            </setup>
            <connection>
                <use>core_setup</use>
            </connection>
        </NameSpace_ModuleName_setup>
      </resources>
   </global>
</config>
Run Code Online (Sandbox Code Playgroud)