Magento 2云2.4.6如何改变mariadb 10.6 utf8mb3到utf8mb4为每列和所有表

hfyxw5xn  于 7个月前  发布在  其他
关注(0)|答案(1)|浏览(95)

1.我有一个基于Magento云项目Magento 2.4.6现在我需要保存字符串到客户名,地址名,甚至帐单地址名。如果没有,它会显示吗?当人们从在线或其他渠道登录时,在前端页面。
1.那么,如果我将每个列从utf8mb3完全更改为utf8mb4,是否会影响功能?
1.是否可以只更改多个表中受影响的列?
1.如果对整体功能没有影响,如何通过代码更改而不是手动更改?

e4yzc0pl

e4yzc0pl1#

我和你在订单项目表上遇到了同样的问题,客户可以在这里输入表情符号来定制文本。当时,我手动更改了该表排序规则,并且在每次setup:upgrade时,我都必须再次更改它。大约一年前,我决定更改所有表的字符集和排序规则,现在仍然很好。所以,我认为在Magento正式支持它之前,为你改变它是没有问题的。
但我有一个第三方模块的问题。其中一个是在db_schema中定义的字符集,没有必要这样做,所以我从db_schema中删除了它。
以下是我的解决方案:
首先,您需要一个自定义模块。
在自定义模块的di文件(位于app/code/Vendor/Module/etc/di.xml中)中,添加以下覆盖:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <!-- ... -->
    <preference for="Magento\Framework\DB\Ddl\Table" type="Vendor\Module\Core"/>
    <type name="Magento\Framework\Setup\Declaration\Schema\Dto\Factories\Table">
        <plugin name="override_default_charset" type="Vendor\Module\Plugin\OverrideDefaultCharsetPlugin" sortOrder="1"
                disabled="false"/>
    </type>

</config>

在模块根目录下创建Core.php文件(app/code/Vendor/Module/Core):

<?php

namespace Vendor\Module;

use Magento\Framework\DB\Ddl\Table;

class Core extends Table
{
    protected $_options = ['type' => 'INNODB', 'charset' => 'utf8mb4', 'collate' => 'utf8mb4_unicode_ci'];
}

您可以更改collate或发动机类型。
然后,您需要创建一个插件来覆盖默认字符集。在app/code/Vendor/Module/Plugin中创建OverrideDefaultCharsetPlugin.php

<?php

declare(strict_types=1);

namespace Vendor\Module\Plugin;

use Magento\Framework\Setup\Declaration\Schema\Dto\Factories\Table;

class OverrideDefaultCharsetPlugin
{
    private const DEFAULT_CHARSET = 'utf8mb4';

    // You can change the default Collation as you need.
    private const DEFAULT_COLLATION = 'utf8mb4_unicode_ci';

    /**
     * @param Table $subject
     * @param array $data
     * @return array
     */
    public function beforeCreate(Table $subject, array $data): array
    {
        if (!isset($data['charset'])) {
            $data['charset'] = self::DEFAULT_CHARSET;
        }

        if (!isset($data['collation'])) {
            $data['collation'] = self::DEFAULT_COLLATION;
        }

        return [$data];
    }
}

最后运行bin/magento setup:upgradebin/magento setup:di:compile

相关问题