在Magento 2中-如何删除customer_entity表中的列?


Answers:


14

在您的设置脚本中,我们可以使用dropColumn

 $setup->getConnection()->dropColumn($setup->getTable('your_table'), 'your_column');

这样做UpgradeSchema.php 是一个更好,更清洁的解决方案
Blueblazer172 '17

7

这是我的解决方案,可能对其他人很有帮助。
我创建了Tohq\Customer\Setup\UpgradeSchema.php文件:

<?php

namespace Tohq\Customer\Setup;

use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;

/**
 * @codeCoverageIgnore
 */
class UpgradeSchema implements UpgradeSchemaInterface
{
    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {            
        // Version of module in setup table is less then the give value.
        if (version_compare($context->getVersion(), '0.1.4', '<')) {

            // get table customer_entity
            $eavTable = $setup->getTable('customer_entity');

            // Check if the table already exists
            if ($setup->getConnection()->isTableExists($eavTable) == true) {
                $connection = $setup->getConnection();

                // del_flg = column name which you want to delete
                $connection->dropColumn($eavTable, 'del_flg');
            }
        }

    }
}

6

您可以在安装脚本中尝试使用此简单的dropColumn()函数。

$this->startSetup();

//example: 
$this->getConnection()->dropColumn($this->getTable('your_table_definition'), 'your column name', $schemaName = null)

$this->endSetup();

谢谢您的回答!
MrTo-Kane

2

路径:Magento22 / app / code / Aks / Training / etc / module.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Aks_Training" setup_version="1.0.2"/>
</config>

在模块中的Setup文件夹下创建UpgradeSchema.php文件。

路径:Magento22 / app / code / Aks / Training / Setup / UpgradeSchema.php

<?php
    namespace Aks\Training\Setup;

    use Magento\Framework\Setup\ModuleContextInterface;
    use Magento\Framework\Setup\SchemaSetupInterface;
    use Magento\Framework\Setup\UpgradeSchemaInterface;

    /**
     * Upgrade the Sales_Order Table to remove extra field
     */
    class UpgradeSchema implements UpgradeSchemaInterface
    {

        public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
        {
            $setup->startSetup();
            if (version_compare($context->getVersion(), '1.0.2', '<')) {
                $setup->getConnection()->dropColumn($setup->getTable('sales_order'), 'gst');
            }
            $setup->endSetup();
        }
    }

我希望它永远有效

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.