From fadd0a338fa4b2a680b6edefafc7c6b9f3240017 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Wed, 8 Jun 2016 10:58:44 +0300 Subject: [PATCH] add createConnection static method --- lib/Doctrine/ORM/EntityManager.php | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/Doctrine/ORM/EntityManager.php b/lib/Doctrine/ORM/EntityManager.php index a38ebecbf..ba882df80 100644 --- a/lib/Doctrine/ORM/EntityManager.php +++ b/lib/Doctrine/ORM/EntityManager.php @@ -836,19 +836,38 @@ use Doctrine\Common\Util\ClassUtils; throw ORMException::missingMappingDriverImpl(); } + $conn = static::createConnection($conn, $config, $eventManager); + + return new EntityManager($conn, $config, $conn->getEventManager()); + } + + /** + * Factory method to create Connection instances. + * + * @param mixed $conn An array with the connection parameters or an existing Connection instance. + * @param Configuration $config The Configuration instance to use. + * @param EventManager $eventManager The EventManager instance to use. + * + * @return Connection + * + * @throws ORMException + * @throws \Doctrine\DBAL\DBALException + */ + protected static function createConnection($conn, Configuration $config, EventManager $eventManager = null) + { if (is_array($conn)) { $conn = DriverManager::getConnection( $conn, $config, ($eventManager ?: new EventManager()) ); } elseif ($conn instanceof Connection) { if ($eventManager !== null && $conn->getEventManager() !== $eventManager) { - throw ORMException::mismatchedEventManager(); + throw ORMException::mismatchedEventManager(); } } else { throw new \InvalidArgumentException("Invalid argument: " . $conn); } - return new EntityManager($conn, $config, $conn->getEventManager()); + return $conn; } /**