我有一个基于magento 2.4.6的magento云项目 现在我需要将表情符号字符串保存到客户名字、地址名字、甚至帐单地址名字中。如果没有的话会显示???当人们从线路或其他渠道登录时在前端页面。
那么如果我将每一列从 utf8mb3 完全更改为 utf8mb4 是否会影响功能?
可以只更改几个表中受影响的列吗?
如果不影响整体功能,如何通过代码更改而不是手动更改?
首先,您需要一个自定义模块。
在自定义模块的 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
或发动机类型。
然后,您需要创建一个插件来覆盖默认字符集。在
OverrideDefaultCharsetPlugin.php
中创建 app/code/Vendor/Module/Plugin
:
<?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';
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:upgrade
和bin/magento setup:di:compile
。