public function testMysql() { $this->assertEquals(self::$dsn->parse('mysql://user@unix(/path/to/socket)/pear'), array('phptype' => 'mysql', 'dbsyntax' => 'mysql', 'protocol' => 'unix', 'username' => 'user', 'database' => 'pear', 'socket' => '/path/to/socket')); $this->assertEquals(self::$dsn->parse('mysql:dbname=testdb;unix_socket=/path/to/socket'), array('phptype' => 'mysql', 'dbsyntax' => 'mysql', 'database' => 'testdb', 'protocol' => 'unix', 'socket' => '/path/to/socket')); $this->assertEquals(self::$dsn->parse('mysql:host=localhost;port=3307;dbname=testdb'), array('phptype' => 'mysql', 'dbsyntax' => 'mysql', 'database' => 'testdb', 'hostspec' => 'localhost', 'protocol' => 'tcp', 'port' => '3307')); $this->assertEquals(self::$dsn->parse('mysqli://*****:*****@localhost/pear?key=client-key.pem&cert=client-cert.pem'), array('phptype' => 'mysqli', 'dbsyntax' => 'mysqli', 'hostspec' => 'localhost', 'protocol' => 'tcp', 'database' => 'pear', 'username' => 'user', 'password' => 'pass', 'key' => 'client-key.pem', 'cert' => 'client-cert.pem')); }
/** * @param string|mixed|\ $dsn * @param array $options * * @return AdapterInterface * * @link http://docs.doctrine-project.org/en/2.0.x/reference/configuration.html * @link http://docs.doctrine-project.org/en/latest/reference/tools.html * @link https://github.com/itmcd/StormIgniter/blob/master/app/libraries/DoctrineDb.php * @link http://framework.zend.com/manual/2.2/en/user-guide/database-and-models.html * @link http://piotrdeszynski.com/zend-framework-2-and-doctrine-2-integration.html * @link http://www.jasongrimes.org/2012/01/using-doctrine-2-in-zend-framework-2/#toc-update-the-album-controller-to-use-doctrine-instead-of-zend_db * @link http://framework.zend.com/manual/2.2/en/user-guide/database-and-models.html */ public function __construct($conn, $options = array()) { $isDev = Environment::isDev(); // @see Doctrine\ORM\EntityManager#create() $config = ArrayUtils::arrayMergeRecursiveRight($options, array('cache' => $isDev ? new Cache\ArrayCache() : new Cache\ApcCache(), 'queryCache' => $isDev ? new Cache\ArrayCache() : new Cache\ApcCache(), 'modelsProxiesPath' => getcwd() . '/cache/ModelProxies')); // create Configuration object if (is_array($options)) { $config = $this->renderConfig($options); if (!$config->getMetadataDriverImpl()) { throw ORMException::missingMappingDriverImpl(); } } // test EventManager object $eventManager = empty($options['eventManager']) ? new EventManager() : $options['eventManager']; switch (true) { case $conn instanceof \PDO: $conn = array('driver' => 'pdo_' . $conn->getAttribute(\PDO::ATTR_DRIVER_NAME), 'pdo' => $conn); $conn = \Doctrine\DBAL\DriverManager::getConnection($conn, $config, $eventManager); break; case is_string($conn): if (($pos = strpos($conn, ':')) !== false) { $driver = substr($conn, 0, $pos); // @TODO: must implement like this, but for now throws an error about not knowing the database // if ($driver !== 'oci8') { // $conn = array( // 'driver' => 'pdo_' . $driver, // 'pdo' => new \PDO($conn), // ); // } else { // $conn = $this->mapDsnToDoctrine($conn); // } // ^ by dragosc@itmcd.ro / temporary replacement untill we discover why \PDO doesn't work $dsn = new Dsn($conn); $conn = $this->mapDsnToDoctrine($dsn->parse()); if ($driver !== 'oci8') { $conn['driver'] = 'pdo_' . $conn['driver']; } // ^ END } $conn = \Doctrine\DBAL\DriverManager::getConnection($conn, $config, $eventManager); break; case is_array($conn): $conn = \Doctrine\DBAL\DriverManager::getConnection($conn, $config, $eventManager); break; case $conn instanceof Connection: if ($eventManager !== null && $conn->getEventManager() !== null) { throw ORMException::mismatchedEventManager(); } break; default: throw new AthemException\InvalidArgument("Invalid argument: " . json_encode($conn)); } // parent constructor // @see Doctrine\ORM\EntityManager#__construct() parent::__construct($conn, $config, $conn->getEventManager()); }