@@ -149,6 +149,16 @@ class Mysql extends \Zend_Db_Adapter_Pdo_Mysql implements AdapterInterface, Rese
149
149
*/
150
150
private const COLUMN_TYPE = ['varchar ' , 'char ' , 'text ' , 'mediumtext ' , 'longtext ' ];
151
151
152
+ /**
153
+ * const for charset
154
+ */
155
+ private const CHARSET = "utf8mb4 " ;
156
+
157
+ /**
158
+ * const for collation
159
+ */
160
+ private const COLLATION = "utf8mb4_general_ci " ;
161
+
152
162
/**
153
163
* MySQL column - Table DDL type pairs
154
164
*
@@ -1258,7 +1268,11 @@ public function modifyColumn($tableName, $columnName, $definition, $flushData =
1258
1268
if (is_array ($ definition )) {
1259
1269
$ definition = $ this ->_getColumnDefinition ($ definition );
1260
1270
}
1261
-
1271
+ // Add charset and collation for DBC failures
1272
+ if (!empty ($ definition )) {
1273
+ $ type = explode (' ' , trim ($ definition ));
1274
+ $ definition = $ this ->applyCharsetAndCollation ($ type [0 ], $ definition , 1 );
1275
+ }
1262
1276
$ sql = sprintf (
1263
1277
'ALTER TABLE %s MODIFY COLUMN %s %s ' ,
1264
1278
$ this ->quoteIdentifier ($ tableName ),
@@ -2433,20 +2447,13 @@ protected function _getColumnsDefinition(Table $table)
2433
2447
$ columnDefinition
2434
2448
);
2435
2449
}
2436
-
2450
+ // Adding charset and collation for DBC failures
2437
2451
if (count ($ definition )) {
2438
2452
foreach ($ definition as $ index => $ columnDefinition ) {
2439
2453
$ type = explode (' ' , trim ($ columnDefinition ));
2440
- $ pattern = '/\b( ' . implode ('| ' , array_map ('preg_quote ' , self ::COLUMN_TYPE )) . ')\b/i ' ;
2441
- if (preg_match ($ pattern , $ type [1 ]) === 1 ) {
2442
- $ charsets = 'CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci ' ;
2443
- $ columnsAttribute = explode (' ' , trim ($ columnDefinition ));
2444
- array_splice ($ columnsAttribute , 2 , 0 , $ charsets );
2445
- $ definition [$ index ] = implode (' ' , $ columnsAttribute );
2446
- }
2454
+ $ definition [$ index ] = $ this ->applyCharsetAndCollation ($ type [1 ], $ columnDefinition , 2 );
2447
2455
}
2448
2456
}
2449
-
2450
2457
// PRIMARY KEY
2451
2458
if (!empty ($ primary )) {
2452
2459
asort ($ primary , SORT_NUMERIC );
@@ -4301,4 +4308,24 @@ public function __debugInfo()
4301
4308
{
4302
4309
return [];
4303
4310
}
4311
+
4312
+ /***
4313
+ * Adding charset and collation for DBC failures
4314
+ *
4315
+ * @param $columnType
4316
+ * @param $definition
4317
+ * @param $position
4318
+ * @return string
4319
+ */
4320
+ private function applyCharsetAndCollation ($ columnType , $ definition , $ position ) : string
4321
+ {
4322
+ $ pattern = '/\b( ' . implode ('| ' , array_map ('preg_quote ' , self ::COLUMN_TYPE )) . ')\b/i ' ;
4323
+ if (preg_match ($ pattern , $ columnType ) === 1 ) {
4324
+ $ charsets = 'CHARACTER SET ' . self ::CHARSET . ' COLLATE ' . self ::COLLATION ;
4325
+ $ columnsAttribute = explode (' ' , trim ($ definition ));
4326
+ array_splice ($ columnsAttribute , $ position , 0 , $ charsets );
4327
+ return implode (' ' , $ columnsAttribute );
4328
+ }
4329
+ return $ definition ;
4330
+ }
4304
4331
}
0 commit comments