2

Magento 2.3.1

在以下位置制作数据补丁后:

vendor/ModuleName/Setup/Patch/Data/AddMyColumnPatch.php

(下面给出的代码AddMyColumnPatch.php

当我运行bin/magento setup:upgrade安装此补丁时,我在 cli 处收到以下错误:

事务中不允许使用 DDL 语句

我使用以下文件作为参考,使用数据补丁将列添加到我的表中:

vendor/magento/module-quote/Setup/Patch/Data/InstallEntityTypes.php

从 47 到 65 行。

我的AddMyColumnPatch.php代码是:

<?php
namespace Vendor\ModuleName\Setup\Patch\Data;


use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\PatchRevertableInterface;

use Magento\Quote\Setup\QuoteSetupFactory;
use Magento\Sales\Setup\SalesSetupFactory;

use Psr\Log\LoggerInterface;

/**
 */
class AddressSuburbPatch implements DataPatchInterface, PatchRevertableInterface
{
    /**
     * Attribute Code of the Custom Attribute
     */
    const CUSTOM_ATTRIBUTE_CODE = 'my_column';

    /**
     * @var \Magento\Framework\Setup\ModuleDataSetupInterface
     */
    private $moduleDataSetup;

    /**
     * @var \Magento\Quote\Setup\QuoteSetupFactory
     */
    private $quoteSetupFactory;


    /**
     * @var Magento\Sales\Setup\SalesSetupFactory
     */
    private $salesSetupFactory;

    /**
     * @var \Psr\Log\LoggerInterface
     */
    private $logger;

    /**
     * @param \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup
     */
    public function __construct(
        ModuleDataSetupInterface $moduleDataSetup,
        QuoteSetupFactory $quoteSetupFactory,
        SalesSetupFactory $salesSetupFactory,    
        LoggerInterface $logger
    )
    {

        $this->moduleDataSetup = $moduleDataSetup;          
        $this->quoteSetupFactory = $quoteSetupFactory;
        $this->salesSetupFactory = $salesSetupFactory;            
        $this->logger = $logger;
    }

    /**
     * {@inheritdoc}
     */
    public function apply()
    {
        $this->moduleDataSetup->getConnection()->startSetup();


        $this->logger->debug('DDL Statements error');

        $quoteSetup = $this->quoteSetupFactory->create(['setup' => $this->moduleDataSetup]);

        $quoteSetup->addAttribute('quote_address', self::CUSTOM_ATTRIBUTE_CODE, ['type' => 'text']);

        $salesSetup = $this->salesSetupFactory->create(['setup' => $this->moduleDataSetup]);
        $salesSetup->addAttribute('order_address', self::CUSTOM_ATTRIBUTE_CODE, ['type' => 'text']);

        $this->logger->debug('Script working');

        $this->moduleDataSetup->getConnection()->endSetup();

    }

    /**
     * {@inheritdoc}
     */
    public static function getDependencies()
    {

        return [

        ];
    }

    public function revert()
    {
        $this->moduleDataSetup->getConnection()->startSetup();

        $this->moduleDataSetup->getConnection()->endSetup();
    }

    /**
     * {@inheritdoc}
     */
    public function getAliases()
    {

        return [];
    }
}
4

1 回答 1

2

在再次浏览声明性架构文档并为报价模块和 PayPal 模块引用核心 Magento 代码后,我发现如果您想将字段添加到 Magento >=2.3 中的现有表中,您应该为此使用配置声明性架构。阅读更多:

https://devdocs.magento.com/guides/v2.4/extension-dev-guide/declarative-schema/db-schema.html

所以在下创建一个db_schema.xml文件Vendor/ModuleName/etc

<?xml version="1.0"?>
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
    <table name="quote_address" resource="checkout" comment="Sales Flat Quote Address">
        <column xsi:type="varchar" name="suburb" nullable="true" length="255" comment="Suburb for Quote Address" />
    </table>
    <table name="sales_order_address" resource="sales" comment="Sales Flat Order Address">
        <column xsi:type="varchar" name="mycolumn" nullable="true" length="255" comment="mycolumn for Sales Order Address" />
    </table>
</schema>

然后为您的 db_schema 生成白名单,如下所示:

bin/magento setup:db-declaration:generate-whitelist --module-name=Vendor_ModuleName

再次运行,您的列将被添加到quote_addressorder_sales_address中。

bin/magento setup:upgrade

然而,进一步的调查表明,在平面表quote_addresssales_order_address. 只有在其中声明列db_schema.xml才能完成这项工作。

于 2019-06-07T06:52:42.117 回答