8
8
use Doctrine \Common \EventManager ;
9
9
use Doctrine \DBAL \Connection as DbalConnection ;
10
10
use Doctrine \DBAL \Driver \Connection as DbalDriverConnection ;
11
+ use Doctrine \DBAL \Driver \Mysqli \Connection as DbalMysqliConnection ;
11
12
use Doctrine \DBAL \DriverManager ;
12
13
use Doctrine \DBAL \Platforms \AbstractPlatform ;
13
14
use Doctrine \DBAL \Platforms \OraclePlatform ;
@@ -35,6 +36,7 @@ abstract class Connection
35
36
protected static $ connectionClassRegistry = [
36
37
'pdo_sqlite ' => Sqlite \Connection::class,
37
38
'pdo_mysql ' => Mysql \Connection::class,
39
+ 'mysqli ' => Mysql \Connection::class,
38
40
'pdo_pgsql ' => Postgresql \Connection::class,
39
41
'pdo_oci ' => Oracle \Connection::class,
40
42
'pdo_sqlsrv ' => Mssql \Connection::class,
@@ -129,7 +131,7 @@ public static function normalizeDsn($dsn, $user = null, $password = null)
129
131
$ dsn ['password ' ] = $ password ;
130
132
}
131
133
132
- if (!str_starts_with ($ dsn ['driver ' ], 'pdo_ ' )) {
134
+ if (!str_starts_with ($ dsn ['driver ' ], 'pdo_ ' ) && ! in_array ( $ dsn [ ' driver ' ], [ ' mysqli ' ], true ) ) {
133
135
$ dsn ['driver ' ] = 'pdo_ ' . $ dsn ['driver ' ];
134
136
}
135
137
@@ -197,14 +199,35 @@ final public static function isComposerDbal2x(): bool
197
199
return !class_exists (DbalResult::class);
198
200
}
199
201
200
- private static function getDriverNameFromDbalDriverConnection (DbalDriverConnection $ connection ): string
202
+ private static function getDriverFromDbalDriverConnection (DbalDriverConnection $ connection ): object
201
203
{
202
- while (self ::isComposerDbal2x () ? $ connection instanceof \ PDO : $ connection = $ connection -> getWrappedConnection ()) {
203
- if ($ connection instanceof \PDO ) {
204
- return ' pdo_ ' . $ connection-> getAttribute (\ PDO :: ATTR_DRIVER_NAME ) ;
204
+ if (self ::isComposerDbal2x ()) {
205
+ if ($ connection instanceof \PDO || $ connection instanceof \mysqli ) {
206
+ return $ connection ;
205
207
}
206
208
}
207
209
210
+ $ wrappedConnection = $ connection instanceof DbalMysqliConnection
211
+ ? $ connection ->getWrappedResourceHandle ()
212
+ : $ connection ->getWrappedConnection (); // @phpstan-ignore-line
213
+
214
+ if ($ wrappedConnection instanceof \PDO || $ wrappedConnection instanceof \mysqli) {
215
+ return $ wrappedConnection ;
216
+ }
217
+
218
+ return self ::getDriverFromDbalDriverConnection ($ wrappedConnection );
219
+ }
220
+
221
+ private static function getDriverNameFromDbalDriverConnection (DbalDriverConnection $ connection ): string
222
+ {
223
+ $ driver = self ::getDriverFromDbalDriverConnection ($ connection );
224
+
225
+ if ($ driver instanceof \PDO ) {
226
+ return 'pdo_ ' . $ driver ->getAttribute (\PDO ::ATTR_DRIVER_NAME );
227
+ } elseif ($ driver instanceof \mysqli) {
228
+ return 'mysqli ' ;
229
+ }
230
+
208
231
return null ; // @phpstan-ignore-line
209
232
}
210
233
@@ -216,7 +239,7 @@ protected static function createDbalEventManager(): EventManager
216
239
protected static function connectFromDsn (array $ dsn ): DbalDriverConnection
217
240
{
218
241
$ dsn = static ::normalizeDsn ($ dsn );
219
- if ($ dsn ['driver ' ] === 'pdo_mysql ' ) {
242
+ if ($ dsn ['driver ' ] === 'pdo_mysql ' || $ dsn [ ' driver ' ] === ' mysqli ' ) {
220
243
$ dsn ['charset ' ] = 'utf8mb4 ' ;
221
244
} elseif ($ dsn ['driver ' ] === 'pdo_oci ' ) {
222
245
$ dsn ['charset ' ] = 'AL32UTF8 ' ;
@@ -413,7 +436,9 @@ public function rollBack(): void
413
436
*/
414
437
public function lastInsertId (string $ sequence = null ): string
415
438
{
416
- return $ this ->connection ()->lastInsertId ($ sequence );
439
+ $ res = $ this ->connection ()->lastInsertId ($ sequence );
440
+
441
+ return is_int ($ res ) ? (string ) $ res : $ res ;
417
442
}
418
443
419
444
public function getDatabasePlatform (): AbstractPlatform
0 commit comments