From 47b93ca03ad0fc130348dd8f8b006b8ef196bb66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=AF=E7=AB=8B?= Date: Sat, 16 Aug 2025 00:10:04 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=9B=B4=E6=96=B0=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E5=90=8C=E6=AD=A5=E8=84=9A=E6=9C=AC=E7=B4=A2=E5=BC=95?= =?UTF-8?q?=E9=95=BF=E5=BA=A6=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../think-library/src/extend/PhinxExtend.php | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/plugin/think-library/src/extend/PhinxExtend.php b/plugin/think-library/src/extend/PhinxExtend.php index 0fd3ae3fa..a4bb9bdf3 100644 --- a/plugin/think-library/src/extend/PhinxExtend.php +++ b/plugin/think-library/src/extend/PhinxExtend.php @@ -115,10 +115,9 @@ class PhinxExtend } } // 生成索引规则 - $short = substr(md5($table->getName()), 0, 9); foreach ($indexs as $field) { if (empty($isExists) || !$table->hasIndex($field)) { - $table->addIndex($field, ['name' => "i{$short}_{$field}"]); + $table->addIndex($field, ['name' => self::genIndexName($table->getName(), $field)]); } } $isExists ? $table->update() : $table->create(); @@ -128,6 +127,27 @@ class PhinxExtend return $table; } + /** + * 生成索引名称. + * + * 生成规则: idx_[表名hash后4位]_[表名缩写]_[字段缩写] + * 缩写规则: 取每个下划线分隔部分的第一个字母 + * + * @param string $table 表名 + * @param string $name 字段名 + * @return string 生成的索引名称 + */ + private static function genIndexName(string $table, string $name): string + { + $getInitials = function (string $str): string { + return implode('', array_map(function ($part) { + return $part[0] ?? ''; + }, explode('_', $str))); + }; + return sprintf('idx_%s_%s_%s', substr(md5($table), -4), $getInitials($table), $getInitials($name)); + } + + /** * 创建数据库安装脚本 * @param array $tables