/** * Creates a new EntityManager that operates on the given database connection * and uses the given Configuration and EventManager implementations. * * @param Doctrine\DBAL\Connection $conn * @param string $name * @param Doctrine\ORM\Configuration $config * @param Doctrine\Common\EventManager $eventManager */ protected function __construct(Connection $conn, Configuration $config, EventManager $eventManager) { $this->_conn = $conn; $this->_config = $config; $this->_eventManager = $eventManager; $this->_metadataFactory = new ClassMetadataFactory($this->_config->getMetadataDriverImpl(), $this->_conn->getDatabasePlatform()); $this->_metadataFactory->setCacheDriver($this->_config->getMetadataCacheImpl()); $this->_unitOfWork = new UnitOfWork($this); $this->_proxyGenerator = new DynamicProxyGenerator($this, $this->_config->getCacheDir()); }
/** * Initialize auto loader of Doctrine. * * @return \Doctrine\ORM\EntityManager */ public static function init($config) { self::optionsContainDSN($config); $vendor = VENDOR_PATH . '/doctrine/common/lib/'; // ensure doctrine2 exists in the libraries folder if (is_file($vendor . 'Doctrine/Common/ClassLoader.php') === false) { throw new \Koch\Exception\Exception('Doctrine2 not found. Check Libraries Folder.', 100); } // get isolated loader require $vendor . 'Doctrine/Common/ClassLoader.php'; // setup autoloaders with namespace and path to search in $classLoader = new \Doctrine\Common\ClassLoader('Doctrine', VENDOR_PATH); $classLoader->register(); $classLoader = new \Doctrine\Common\ClassLoader('Symfony', VENDOR_PATH . 'Doctrine/Symfony'); $classLoader->register(); $classLoader = new \Doctrine\Common\ClassLoader('Entity', APPLICATION_PATH . 'Doctrine'); $classLoader->register(); $classLoader = new \Doctrine\Common\ClassLoader('Repository', APPLICATION_PATH . 'Doctrine'); $classLoader->register(); $classLoader = new \Doctrine\Common\ClassLoader('Proxy', APPLICATION_PATH . 'Doctrine'); $classLoader->register(); // include Doctrine Extensions $classLoader = new \Doctrine\Common\ClassLoader('doctrine-extensions', VENDOR_PATH . 'gedmo/doctrine-extensions/lib/Gedmo'); $classLoader->register(); $classLoader = new \Doctrine\Common\ClassLoader('DoctrineExtensions', VENDOR_PATH . 'beberlei/DoctrineExtensions/lib'); $classLoader->register(); // fetch doctrine config handler for configuring $D2Config = new \Doctrine\ORM\Configuration(); // fetch cache driver - APC in production and Array in development mode if (extension_loaded('apc') and DEBUG === false) { $cache = new \Doctrine\Common\Cache\ApcCache(); } else { $cache = new \Doctrine\Common\Cache\ArrayCache(); } // set cache driver $D2Config->setMetadataCacheImpl($cache); $D2Config->setQueryCacheImpl($cache); // set annotation driver for entities $D2Config->setMetadataDriverImpl($D2Config->newDefaultAnnotationDriver(self::getModelPathsForAllModules())); /* * This is slow like hell, because getAllClassNames traverses all * dirs and files and includes them. Its a workaround, till i find * a better way to acquire all the models. * @todo optimize this for performance reasons */ $D2Config->getMetadataDriverImpl()->getAllClassNames(); #\Koch\Debug\Debug::firebug($config->getMetadataDriverImpl()->getAllClassNames()); // set proxy dirs $D2Config->setProxyDir(APPLICATION_PATH . 'Doctrine'); $D2Config->setProxyNamespace('Proxy'); // regenerate proxies only in debug and not in production mode if (DEBUG === true) { $D2Config->setAutoGenerateProxyClasses(true); } else { $D2Config->setAutoGenerateProxyClasses(false); } // use main configuration values for setting up the connection $connectionOptions = ['driver' => $config['database']['driver'], 'user' => $config['database']['user'], 'password' => $config['database']['password'], 'dbname' => $config['database']['dbname'], 'host' => $config['database']['host'], 'charset' => $config['database']['charset'], 'driverOptions' => ['charset' => $config['database']['charset']]]; // set up Logger #$config->setSqlLogger(new \Doctrine\DBAL\Logging\EchoSqlLogger); /* * Events */ $event = new \Doctrine\Common\EventManager(); /* * Database Prefix * * The constant definition is for building (raw) sql queries manually. * The database prefixing is registered via an event. */ define('DB_PREFIX', $config['database']['prefix']); $tablePrefix = new TablePrefix(DB_PREFIX); $event->addEventListener(\Doctrine\ORM\Events::loadClassMetadata, $tablePrefix); /* * Custom Functions * * We need some more functions for MySQL, like RAND for random values. */ $D2Config->addCustomNumericFunction('RAND', 'Koch\\Doctrine\\Extensions\\Query\\Mysql\\Rand'); // Entity manager $em = \Doctrine\ORM\EntityManager::create($connectionOptions, $D2Config, $event); // set DBAL DebugStack Logger (also needed for counting queries) if (defined('DEBUG') and DEBUG === 1) { self::$sqlLoggerStack = new \Doctrine\DBAL\Logging\DebugStack(); $em->getConfiguration()->setSQLLogger(self::$sqlLoggerStack); // Echo SQL Queries directly on the page. $em->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger()); } self::$em = $em; // the D2 initalization is done, remove vars to safe memory unset($config, $em, $event, $cache, $classLoader, $D2Config); return self::$em; }
/** * Initialize auto loader of Doctrine * * @return Doctrine_Enitity_Manager */ public static function init($clansuite_config) { self::checkDataSourceName($clansuite_config); // ensure doctrine2 exists in the libraries folder if (is_file(ROOT_LIBRARIES . 'Doctrine/Common/ClassLoader.php') === false) { throw new Koch_Exception('Doctrine2 not found. Check Libraries Folder.', 100); } // get isolated loader require ROOT_LIBRARIES . 'Doctrine/Common/ClassLoader.php'; // setup autoloaders with namespace and path to search in $classLoader = new \Doctrine\Common\ClassLoader('Doctrine', realpath(ROOT_LIBRARIES)); $classLoader->register(); $classLoader = new \Doctrine\Common\ClassLoader('Symfony', realpath(ROOT_LIBRARIES . 'Doctrine/Symfony')); $classLoader->register(); $classLoader = new \Doctrine\Common\ClassLoader('Entities', realpath(ROOT . 'doctrine')); $classLoader->register(); $classLoader = new \Doctrine\Common\ClassLoader('Repositories', realpath(ROOT . 'doctrine')); $classLoader->register(); $classLoader = new \Doctrine\Common\ClassLoader('Proxies', realpath(ROOT . 'doctrine')); $classLoader->register(); // include Doctrine Extensions $classLoader = new \Doctrine\Common\ClassLoader('DoctrineExtensions', realpath(ROOT_LIBRARIES)); $classLoader->register(); // fetch doctrine config handler for configuring $config = new \Doctrine\ORM\Configuration(); // fetch cache driver - APC in production and Array in development mode if (extension_loaded('apc') and DEBUG == false) { $cache = new \Doctrine\Common\Cache\ApcCache(); } else { $cache = new \Doctrine\Common\Cache\ArrayCache(); } // set cache driver $config->setMetadataCacheImpl($cache); $config->setQueryCacheImpl($cache); // set annotation driver for entities $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver(self::getModelPathsForAllModules())); // @todo workaround till i find a better way to acquire all the models $config->getMetadataDriverImpl()->getAllClassNames(); #Koch_Debug::firebug($config->getMetadataDriverImpl()->getAllClassNames()); // set proxy dirs $config->setProxyDir(realpath(ROOT . 'doctrine')); $config->setProxyNamespace('Proxies'); // regenerate proxies only in debug and not in production mode if (DEBUG == true) { $config->setAutoGenerateProxyClasses(true); } else { $config->setAutoGenerateProxyClasses(false); } // use main configuration values for setting up the connection $connectionOptions = array('driver' => $clansuite_config['database']['driver'], 'user' => $clansuite_config['database']['user'], 'password' => $clansuite_config['database']['password'], 'dbname' => $clansuite_config['database']['dbname'], 'host' => $clansuite_config['database']['host'], 'charset' => $clansuite_config['database']['charset'], 'driverOptions' => array('charset' => $clansuite_config['database']['charset'])); // set up Logger #$config->setSqlLogger(new \Doctrine\DBAL\Logging\EchoSqlLogger); // we need some more functions for mysql $config->addCustomNumericFunction('RAND', 'DoctrineExtensions\\Query\\Mysql\\Rand'); /** * Events */ $event = new \Doctrine\Common\EventManager(); /** * Database Prefix * * The constant definition is for building (raw) sql queries manually. * The database prefixing is registered via an event. */ define('DB_PREFIX', $clansuite_config['database']['prefix']); $tablePrefix = new \DoctrineExtensions\TablePrefix\TablePrefix(DB_PREFIX); $event->addEventListener(\Doctrine\ORM\Events::loadClassMetadata, $tablePrefix); /** * Custom Functions * * We need some more functions for mysql, like RAND for random values. */ $config->addCustomNumericFunction('RAND', 'DoctrineExtensions\\Query\\Mysql\\Rand'); /** * Set UTF-8 handling of database data via Doctrine Event for MySQL. */ if ($clansuite_config['database']['driver'] !== null and $clansuite_config['database']['driver'] == "pdo_mysql") { /** * @todo eval database.charset true? * wouldn't it be better to use utf-8 to name it explicitly */ if ($clansuite_config['database']['charset'] !== null) { $event->addEventSubscriber(new \Doctrine\DBAL\Event\Listeners\MysqlSessionInit($clansuite_config['database']['charset'], 'utf8_unicode_ci')); } } // Entity manager $em = \Doctrine\ORM\EntityManager::create($connectionOptions, $config, $event); // set DBAL DebugStack Logger (also needed for counting queries) if (defined('DEBUG') and DEBUG == 1) { self::$sqlLoggerStack = new \Doctrine\DBAL\Logging\DebugStack(); $em->getConfiguration()->setSQLLogger(self::$sqlLoggerStack); /** * Echo SQL Queries directly on the page. */ #$em->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger()); } self::$em = $em; // done with config, remove to safe memory unset($clansuite_config, $em, $event); return self::$em; }