/** * Init the doctrine entity manager. * * @return Doctrine\ORM\EntityManager */ private function _initEntityManager() { // New database configuration $config = new Configuration(); $driverImpl = $config->newDefaultAnnotationDriver(); $config->setMetadataDriverImpl($driverImpl); $proxiesPath = $this->_application->getCacheDir() . DIRECTORY_SEPARATOR . 'Proxies'; $config->setProxyDir($proxiesPath); $config->setProxyNamespace('Proxies'); // Create EntityManager $em = EntityManager::create($this->_config, $config); if (isset($this->_config['charset'])) { try { $em->getConnection()->executeUpdate('SET SESSION character_set_client = "' . addslashes($this->_config['charset']) . '";'); $em->getConnection()->executeUpdate('SET SESSION character_set_connection = "' . addslashes($this->_config['charset']) . '";'); $em->getConnection()->executeUpdate('SET SESSION character_set_results = "' . addslashes($this->_config['charset']) . '";'); } catch (\Exception $e) { throw new BBException(sprintf('Invalid database character set `%s`', $this->_config['charset']), BBException::INVALID_ARGUMENT, $e); } } if (isset($this->_config['collation'])) { try { $em->getConnection()->executeUpdate('SET SESSION collation_connection = "' . addslashes($this->_config['collation']) . '";'); } catch (\Exception $e) { throw new BBException(sprintf('Invalid database collation `%s`', $this->_config['collation']), BBException::INVALID_ARGUMENT, $e); } } return $em; }
/** * Initiate doctrine connection for the Command on master database if its configure * * @param object $input The input option of command * @param object $output The output of command * * @throws \DatabaseConnectionException When Unable to connect to database */ protected function initConnection($input, $output) { if (null !== $input->getOption('host')) { $connection['host'] = $input->getOption('host'); } if (null !== $input->getOption('port')) { $connection['port'] = $input->getOption('port'); } if (null !== $input->getOption('user')) { $connection['user'] = $input->getOption('user'); } if (null !== $input->getOption('password')) { $connection['password'] = $input->getOption('password'); } $doctrine_config = $this->bbapp->getConfig()->getDoctrineConfig(); if (isset($connection['user']) && isset($connection['password'])) { if (isset($doctrine_config['dbal']['master'])) { $doctrine_config['dbal']['master'] = array_merge($doctrine_config['dbal']['master'], $connection); } else { $doctrine_config['dbal'] = array_merge($doctrine_config['dbal'], $connection); } } // DISABLE CACHE DOCTRINE unset($doctrine_config['dbal']['metadata_cache_driver']); unset($doctrine_config['dbal']['query_cache_driver']); if (!array_key_exists('proxy_ns', $doctrine_config['dbal'])) { $doctrine_config['dbal']['proxy_ns'] = 'Proxies'; } if (!array_key_exists('proxy_dir', $doctrine_config['dbal'])) { $doctrine_config['dbal']['proxy_dir'] = $this->bbapp->getCacheDir() . '/' . 'Proxies'; } try { $em = EntityManagerCreator::create($doctrine_config['dbal']); if (isset($doctrine_config['dbal']['master'])) { $em->getConnection()->connect('master'); } else { $em->getConnection()->connect(); } } catch (\Exception $e) { throw new DatabaseConnectionException('Unable to connect to the database.', 0, $e); } return $em; }