From 52b33a64c7272ffdb099838948fa4937cf48f100 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=93=AD=E6=98=95?= <715557344@qq.com> Date: Fri, 15 Jan 2021 16:45:00 +0800 Subject: [PATCH 1/2] Fixed doctrine/dbal does not works. --- src/database/src/Connection.php | 9 +- .../src/DBAL/Concerns/ConnectsToDatabase.php | 30 ++++ src/database/src/DBAL/Connection.php | 168 ++++++++++++++++++ src/database/src/DBAL/MySqlDriver.php | 20 +++ src/database/src/MySqlConnection.php | 4 +- 5 files changed, 227 insertions(+), 4 deletions(-) create mode 100644 src/database/src/DBAL/Concerns/ConnectsToDatabase.php create mode 100644 src/database/src/DBAL/Connection.php create mode 100644 src/database/src/DBAL/MySqlDriver.php diff --git a/src/database/src/Connection.php b/src/database/src/Connection.php index c367fe3377..88461cef7d 100755 --- a/src/database/src/Connection.php +++ b/src/database/src/Connection.php @@ -570,7 +570,12 @@ public function getDoctrineColumn($table, $column) */ public function getDoctrineSchemaManager() { - return $this->getDoctrineDriver()->getSchemaManager($this->getDoctrineConnection()); + $connection = $this->getDoctrineConnection(); + + return $this->getDoctrineDriver()->getSchemaManager( + $connection, + $connection->getDatabasePlatform() + ); } /** @@ -586,7 +591,7 @@ public function getDoctrineConnection() $this->doctrineConnection = new DoctrineConnection([ 'pdo' => $this->getPdo(), 'dbname' => $this->getConfig('database'), - 'driver' => $driver->getName(), + 'driver' => null, ], $driver); } diff --git a/src/database/src/DBAL/Concerns/ConnectsToDatabase.php b/src/database/src/DBAL/Concerns/ConnectsToDatabase.php new file mode 100644 index 0000000000..6c3d5cf357 --- /dev/null +++ b/src/database/src/DBAL/Concerns/ConnectsToDatabase.php @@ -0,0 +1,30 @@ +connection = $connection; + } + + /** + * Execute an SQL statement. + */ + public function exec(string $sql): int + { + try { + $result = $this->connection->exec($sql); + + \assert($result !== false); + + return $result; + } catch (PDOException $exception) { + throw Exception::new($exception); + } + } + + /** + * Prepare a new SQL statement. + */ + public function prepare(string $sql): StatementInterface + { + try { + return $this->createStatement( + $this->connection->prepare($sql) + ); + } catch (PDOException $exception) { + throw Exception::new($exception); + } + } + + /** + * Execute a new query against the connection. + */ + public function query(string $sql): ResultInterface + { + try { + $stmt = $this->connection->query($sql); + + \assert($stmt instanceof PDOStatement); + + return new Result($stmt); + } catch (PDOException $exception) { + throw Exception::new($exception); + } + } + + /** + * Get the last insert ID. + * + * @param null|string $name + * @return string + */ + public function lastInsertId($name = null) + { + try { + if ($name === null) { + return $this->connection->lastInsertId(); + } + + return $this->connection->lastInsertId($name); + } catch (PDOException $exception) { + throw Exception::new($exception); + } + } + + /** + * Begin a new database transaction. + */ + public function beginTransaction() + { + return $this->connection->beginTransaction(); + } + + /** + * Commit a database transaction. + */ + public function commit() + { + return $this->connection->commit(); + } + + /** + * Roll back a database transaction. + */ + public function rollBack() + { + return $this->connection->rollBack(); + } + + /** + * Wrap quotes around the given input. + * + * @param string $input + * @param string $type + * @return string + */ + public function quote($input, $type = ParameterType::STRING) + { + return $this->connection->quote($input, $type); + } + + /** + * Get the server version for the connection. + * + * @return string + */ + public function getServerVersion() + { + return $this->connection->getAttribute(PDO::ATTR_SERVER_VERSION); + } + + /** + * Get the wrapped PDO connection. + */ + public function getWrappedConnection(): PDO + { + return $this->connection; + } + + /** + * Create a new statement instance. + */ + protected function createStatement(PDOStatement $stmt): Statement + { + return new Statement($stmt); + } +} diff --git a/src/database/src/DBAL/MySqlDriver.php b/src/database/src/DBAL/MySqlDriver.php new file mode 100644 index 0000000000..ffc4c6150a --- /dev/null +++ b/src/database/src/DBAL/MySqlDriver.php @@ -0,0 +1,20 @@ + Date: Fri, 15 Jan 2021 16:47:49 +0800 Subject: [PATCH 2/2] Change document --- CHANGELOG-2.1.md | 1 + docs/zh-cn/upgrade/2.1.md | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/CHANGELOG-2.1.md b/CHANGELOG-2.1.md index 4c4f74ecd1..9778645d7b 100644 --- a/CHANGELOG-2.1.md +++ b/CHANGELOG-2.1.md @@ -7,6 +7,7 @@ - [#3118](https://github.com/hyperf/hyperf/pull/3118) Fixed bug that the config key of migrations is not correct. - [#3126](https://github.com/hyperf/hyperf/pull/3126) Fixed bug that swoole v4.6 `SWOOLE_HOOK_SOCKETS` conflicts with jaeger tracing. - [#3137](https://github.com/hyperf/hyperf/pull/3137) Fixed type hint error, when don't set `true` for `PDO::ATTR_PERSISTENT`. +- [#3141](https://github.com/hyperf/hyperf/pull/3141) Fixed `doctrine/dbal` does not works when using migration. ## Added diff --git a/docs/zh-cn/upgrade/2.1.md b/docs/zh-cn/upgrade/2.1.md index 1ef2a0c6df..7544607493 100644 --- a/docs/zh-cn/upgrade/2.1.md +++ b/docs/zh-cn/upgrade/2.1.md @@ -111,6 +111,10 @@ return [ 因为组件 `hyperf/paginator` 已从 `hyperf/database` 依赖中移除。所以在 database 中使用到分页器的同学,还需要额外引入 `hyperf/paginator` 组件。 +## 修改DBAL版本 + +倘若使用了 `doctrine/dbal` 组件,则需要升级到 `^3.0` 版本。 + ## 注意事项 - 尽量不要将老项目的引擎修改为 Swow,如果想要使用 Swow,请尽量在新项目中尝试。因为 Swow 并不是 Swoole 的替代品,所以并不是所有 Swoole 的场景,都能在 Swow 中找到对应的替代方案。