Php magento在现有表中添加列

Php magento在现有表中添加列,php,magento,Php,Magento,我是马根托的新手。我想在newsletter\u subscriber表中添加一列,因此我在app/code/core/mage/newsletter\u setup/中创建了一个新文件mysql4-upgrade-1.6.0.0-1.6.0.1.php <?php $installer = $this; $installer->startSetup(); $installer->getConnection()->addColumn( $this->getT

我是马根托的新手。我想在
newsletter\u subscriber
表中添加一列,因此我在
app/code/core/mage/newsletter\u setup/
中创建了一个新文件
mysql4-upgrade-1.6.0.0-1.6.0.1.php

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

?>

我更新了配置文件:

<modules>
    <Mage_Newsletter>
        <version>1.6.0.0</version> 
    </Mage_Newsletter>
</modules>

1.6.0.0 

它不起作用,请用安装脚本指导我做错了什么,它们将根据模块版本的变化执行

在您的情况下,您的文件名是
mysql4-upgrade-1.6.0.0-
1.6.0.1.php
,而您的版本是
1.6.0.0
。要执行此特定脚本,您需要将版本升级到
1.6.0.1


也就是说,您正在向核心Magento模块添加功能,这是一种糟糕的做法。您应该将其移动到本地池(
app/code/local
)模块中。

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

您必须在
app/code/local/your/module/sql/your_module\u setup/upgrade-0.1.2-0.1.3.php
文件中提到模块升级的正确版本。(这意味着您需要将模块版本从0.1.2升级到0.1.3)。如果您没有使用升级脚本,请记住在module
config.xml
中定义
,安装脚本名称为
mysql4-install-0.1.0.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();
        ?>
下面是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();
        ?>

然后再更改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>

0.1.3 
名称空间_ModuleName
图像\目录\模型\资源\设置
核心单元设置

您只需在dbscripts文件夹中创建一个脚本,然后从终端或web浏览器运行此文件即可

e、 g在
pub/dbscripts/filename.php中保存文件
粘贴此代码并根据需要进行更改

<?php
use Magento\Framework\App\Bootstrap;
require '../../app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
error_reporting(E_ALL);
ini_set('display_errors', 1);
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();

$salesTable = $resource->getTableName('Table_Name');
$sql = "ALTER TABLE ".$salesTable. " ADD `Field_name` varchar(255)";
$connection->query($sql);

echo"Script Run Successfully";