public function initialize($parameters = array()) { parent::initialize($parameters); $schema = $this->getParameter('schema'); $connectionName = $this->getParameter('name'); $connectionOptions = $this->getParameter('options'); $plugins = (array) $this->getParameter('plugins'); $config = new \Doctrine\ORM\Configuration(); $config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache()); $configuration = sfProjectConfiguration::getActive(); $paths = array(); if ($schema) { $paths[] = $schema; } $paths[] = realpath(__DIR__ . '/../config/doctrine'); $paths[] = realpath(sfConfig::get('sf_config_dir') . '/doctrine'); $enabledPlugins = $configuration->getPlugins(); foreach ($configuration->getAllPluginPaths() as $plugin => $path) { if (!in_array($plugin, $enabledPlugins) || !in_array($plugin, $plugins)) { continue; } $paths[] = $path . '/config/doctrine'; } $paths = array_unique($paths); $config->setMetadataDriverImpl(new YamlDriver($paths)); $config->setProxyDir(sfConfig::get('sf_lib_dir') . '/Proxies'); $config->setProxyNamespace('Proxies'); $configuration = sfProjectConfiguration::getActive(); if (sfConfig::get('sf_debug')) { $config->setSqlLogger(new sfDoctrineSqlLogger($configuration->getEventDispatcher())); } $method = sprintf('configureDoctrineConnection%s', $connectionName); $methodExists = method_exists($configuration, $method); if (method_exists($configuration, 'configureDoctrineConnection') && !$methodExists) { $configuration->configureDoctrineConnection($config); } else { if ($methodExists) { $configuration->{$method}($config); } } $this->em = \Doctrine\ORM\EntityManager::create($connectionOptions, $config); if (method_exists($configuration, 'configureEntityManager')) { $configuration->configureEntityManager($this->em); } ActiveEntity::setEntityManager($this->em); // ODM MongoDB $config = new Doctrine\ODM\MongoDB\Configuration(); $config->setProxyDir(sfConfig::get('sf_lib_dir') . '/Proxies'); $config->setProxyNamespace('Proxies'); $reader = new Doctrine\Common\Annotations\AnnotationReader(); $reader->setDefaultAnnotationNamespace('Doctrine\\ODM\\MongoDB\\Mapping\\'); $config->setMetadataDriverImpl(new Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver($reader, sfConfig::get('sf_lib_dir') . DIRECTORY_SEPARATOR . 'Documents')); $this->dm = Doctrine\ODM\MongoDB\DocumentManager::create(new \Doctrine\ODM\MongoDB\Mongo(), $config); }
$driverImpl = new \Doctrine\ORM\Mapping\Driver\YamlDriver(ASCMS_MODEL_PATH . '/yml'); $chainDriverImpl->addDriver($driverImpl, 'Cx\\Model'); $driverImpl = new \Doctrine\ORM\Mapping\Driver\YamlDriver(ASCMS_CORE_PATH . '/ContentManager/Model/Yaml'); $chainDriverImpl->addDriver($driverImpl, 'Cx\\Core\\ContentManager'); //loggable stuff $loggableDriverImpl = $config->newDefaultAnnotationDriver(array(UPDATE_CORE, $doctrineDir . 'Gedmo/Loggable/Entity')); $chainDriverImpl->addDriver($loggableDriverImpl, 'Gedmo\\Loggable'); $loggableListener = new \Cx\Update\core\LoggableListener(); $evm->addEventSubscriber($loggableListener); \Env::set('loggableListener', $loggableListener); //tree stuff $treeListener = new \Gedmo\Tree\TreeListener(); $evm->addEventSubscriber($treeListener); $config->setMetadataDriverImpl($chainDriverImpl); //table prefix $prefixListener = new \DoctrineExtension\TablePrefixListener($_DBCONFIG['tablePrefix']); $evm->addEventListener(\Doctrine\ORM\Events::loadClassMetadata, $prefixListener); //page listener for unique slugs $pageListener = new PageEventListener(); $evm->addEventListener(\Doctrine\ORM\Events::preUpdate, $pageListener); $evm->addEventListener(\Doctrine\ORM\Events::onFlush, $pageListener); $evm->addEventListener(\Doctrine\ORM\Events::postPersist, $pageListener); $evm->addEventListener(\Doctrine\ORM\Events::preRemove, $pageListener); $config->setSqlLogger(new \Cx\Lib\DBG\DoctrineSQLLogger()); $em = \Doctrine\ORM\EntityManager::create($connectionOptions, $config, $evm); //resolve enum, set errors $conn = $em->getConnection(); $conn->setCharset($_DBCONFIG['charset']); $conn->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string'); $conn->getDatabasePlatform()->registerDoctrineTypeMapping('set', 'string'); Env::setEm($em);
/** * @param array $options * @return \Doctrine\ORM\EntityManager */ protected function _createEntityManager() { $options = $this->_options; $cache = $this->_setupCache($options); if (!isset($options['proxyDir']) || !file_exists($options['proxyDir'])) { throw new ZendX_Doctrine2_Exception("No Doctrine2 'proxyDir' option was given, but is required."); } if (!isset($options['proxyNamespace'])) { $options['proxyNamespace'] = 'MyProject/Proxies'; } if (!isset($options['autoGenerateProxyClasses'])) { $options['autoGenerateProxyClasses'] = true; } if (!isset($options['useCExtension'])) { $options['useCExtension'] = false; } $eventManager = new \Doctrine\Common\EventManager(); $config = new \Doctrine\ORM\Configuration(); $config->setMetadataCacheImpl($cache); $config->setQueryCacheImpl($cache); $config->setProxyDir($options['proxyDir']); $config->setProxyNamespace($options['proxyNamespace']); $config->setUseCExtension((bool) $options['useCExtension']); if (!isset($options['connectionOptions']) || !is_array($options['connectionOptions'])) { throw new ZendX_Doctrine2_Exception("Invalid Doctrine DBAL Connection Options given."); } $connectionOptions = $options['connectionOptions']; if (isset($options['sqllogger'])) { if (is_string($options['sqllogger']) && class_exists($options['sqllogger'])) { $logger = new $options['sqllogger'](); if (!$logger instanceof \Doctrine\DBAL\Logging\SqlLogger) { throw new ZendX_Doctrine2_Exception("Invalid SqlLogger class specified, has to implement \\Doctrine\\DBAL\\Logging\\SqlLogger"); } $config->setSqlLogger($logger); } else { throw new ZendX_Doctrine2_Exception("Invalid SqlLogger configuration specified, have to give class string."); } } $conn = \Doctrine\DBAL\DriverManager::getConnection($connectionOptions, $config, $eventManager); $this->_setupMetadataDriver($options, $config, $cache, $conn); $em = \Doctrine\ORM\EntityManager::create($conn, $config, $eventManager); $registry = Zend_Registry::getInstance(); $registry->em = $em; return $em; }
/** * Returns the doctrine entity manager * @return \Doctrine\ORM\EntityManager */ public function getEntityManager() { if ($this->em) { return $this->em; } $config = new \Doctrine\ORM\Configuration(); //$config->setResultCacheImpl($this->cacheDriver); $config->setMetadataCacheImpl($this->cacheDriver); $config->setQueryCacheImpl($this->cacheDriver); $config->setProxyDir(ASCMS_MODEL_PROXIES_PATH); $config->setProxyNamespace('Cx\\Model\\Proxies'); /** * This should be set to true if workbench is present and active. * Just checking for workbench.config is not really a good solution. * Since ConfigurationFactory used by EM caches auto generation * config value, there's no possibility to set this later. */ $config->setAutoGenerateProxyClasses(file_exists(ASCMS_DOCUMENT_ROOT . '/workbench.config')); $connectionOptions = array('pdo' => $this->getPdoConnection(), 'dbname' => $this->db->getName()); $evm = new \Doctrine\Common\EventManager(); $chainDriverImpl = new \Doctrine\ORM\Mapping\Driver\DriverChain(); $driverImpl = new \Cx\Core\Model\Controller\YamlDriver(array(ASCMS_CORE_PATH . '/Core' . '/Model/Yaml')); $chainDriverImpl->addDriver($driverImpl, 'Cx'); //loggable stuff $loggableDriverImpl = $config->newDefaultAnnotationDriver(ASCMS_LIBRARY_PATH . '/doctrine/Gedmo/Loggable/Entity'); $chainDriverImpl->addDriver($loggableDriverImpl, 'Gedmo\\Loggable'); $this->loggableListener = new \Cx\Core\Model\Model\Event\LoggableListener(); $this->loggableListener->setUsername('currently_loggedin_user'); // in real world app the username should be loaded from session, example: // Session::getInstance()->read('user')->getUsername(); $evm->addEventSubscriber($this->loggableListener); $cx = \Cx\Core\Core\Controller\Cx::instanciate(); $sluggableDriverImpl = $config->newDefaultAnnotationDriver($cx->getCodeBaseLibraryPath() . '/doctrine/Gedmo/Sluggable'); $chainDriverImpl->addDriver($sluggableDriverImpl, 'Gedmo\\Sluggable'); $sluggableListener = new \Gedmo\Sluggable\SluggableListener(); // you should set the used annotation reader to listener, // to avoid creating new one for mapping drivers //$sluggableListener->setAnnotationReader($cachedAnnotationReader); $evm->addEventSubscriber($sluggableListener); $timestampableDriverImpl = $config->newDefaultAnnotationDriver($cx->getCodeBaseLibraryPath() . '/doctrine/Gedmo/Timestampable'); $chainDriverImpl->addDriver($timestampableDriverImpl, 'Gedmo\\Timestampable'); $timestampableListener = new \Gedmo\Timestampable\TimestampableListener(); //$timestampableListener->setAnnotationReader($cachedAnnotationReader); $evm->addEventSubscriber($timestampableListener); // Note that LANG_ID and other language constants/variables // have not been set yet! // $langCode = \FWLanguage::getLanguageCodeById(LANG_ID); // \DBG::log("LANG_ID ".LANG_ID.", language code: $langCode"); // -> LOG: LANG_ID LANG_ID, language code: $translatableDriverImpl = $config->newDefaultAnnotationDriver($cx->getCodeBaseLibraryPath() . '/doctrine/Gedmo/Translatable/Entity'); $chainDriverImpl->addDriver($translatableDriverImpl, 'Gedmo\\Translatable'); // RK: Note: // This might have been renamed in newer versions: //$translationListener = new \Gedmo\Translatable\TranslatableListener(); // In this Doctrine version, it is present as: $this->translationListener = new \Gedmo\Translatable\TranslationListener(); // current translation locale should be set from session // or hook later into the listener, // but *before the entity manager is flushed* // TODO: Set default locale from the default language? //$this->translationListener->setDefaultLocale('de_ch'); // Set the current locale (e.g. from the active language) // wherever that's required. //$translationListener->setTranslatableLocale('de_ch'); //$translationListener->setAnnotationReader($cachedAnnotationReader); $evm->addEventSubscriber($this->translationListener); // RK: Note: // This is apparently not yet present in this Doctrine version: //$sortableListener = new \Gedmo\Sortable\SortableListener(); //$sortableListener->setAnnotationReader($cachedAnnotationReader); //$evm->addEventSubscriber($sortableListener); //tree stuff $treeListener = new \Gedmo\Tree\TreeListener(); $evm->addEventSubscriber($treeListener); $config->setMetadataDriverImpl($chainDriverImpl); //table prefix $prefixListener = new \DoctrineExtension\TablePrefixListener($this->db->getTablePrefix()); $evm->addEventListener(\Doctrine\ORM\Events::loadClassMetadata, $prefixListener); $config->setSqlLogger(new \Cx\Lib\DBG\DoctrineSQLLogger()); $em = \Cx\Core\Model\Controller\EntityManager::create($connectionOptions, $config, $evm); //resolve enum, set errors $conn = $em->getConnection(); $conn->setCharset($this->db->getCharset()); $conn->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string'); $conn->getDatabasePlatform()->registerDoctrineTypeMapping('set', 'string'); $this->em = $em; return $this->em; }
/** * Returns the doctrine entity manager * @return \Doctrine\ORM\EntityManager */ public function getEntityManager() { if ($this->em) { return $this->em; } global $objCache; $config = new \Doctrine\ORM\Configuration(); $userCacheEngine = $objCache->getUserCacheEngine(); if (!$objCache->getUserCacheActive()) { $userCacheEngine = \Cx\Core_Modules\Cache\Controller\Cache::CACHE_ENGINE_OFF; } $arrayCache = new \Doctrine\Common\Cache\ArrayCache(); switch ($userCacheEngine) { case \Cx\Core_Modules\Cache\Controller\Cache::CACHE_ENGINE_APC: $cache = new \Doctrine\Common\Cache\ApcCache(); $cache->setNamespace($this->db->getName() . '.' . $this->db->getTablePrefix()); break; case \Cx\Core_Modules\Cache\Controller\Cache::CACHE_ENGINE_MEMCACHE: $memcache = $objCache->getMemcache(); if ($memcache instanceof \Memcache) { $cache = new \Doctrine\Common\Cache\MemcacheCache(); $cache->setMemcache($memcache); } elseif ($memcache instanceof \Memcached) { $cache = new \Doctrine\Common\Cache\MemcachedCache(); $cache->setMemcache($memcache); } $cache->setNamespace($this->db->getName() . '.' . $this->db->getTablePrefix()); break; case \Cx\Core_Modules\Cache\Controller\Cache::CACHE_ENGINE_XCACHE: $cache = new \Doctrine\Common\Cache\XcacheCache(); $cache->setNamespace($this->db->getName() . '.' . $this->db->getTablePrefix()); break; case \Cx\Core_Modules\Cache\Controller\Cache::CACHE_ENGINE_FILESYSTEM: $cache = new \Cx\Core_Modules\Cache\Controller\Doctrine\CacheDriver\FileSystemCache(ASCMS_CACHE_PATH); break; default: $cache = $arrayCache; break; } \Env::set('cache', $cache); //$config->setResultCacheImpl($cache); $config->setMetadataCacheImpl($cache); $config->setQueryCacheImpl($cache); $config->setProxyDir(ASCMS_MODEL_PROXIES_PATH); $config->setProxyNamespace('Cx\\Model\\Proxies'); /** * This should be set to true if workbench is present and active. * Just checking for workbench.config is not really a good solution. * Since ConfigurationFactory used by EM caches auto generation * config value, there's no possibility to set this later. */ $config->setAutoGenerateProxyClasses(file_exists(ASCMS_DOCUMENT_ROOT . '/workbench.config')); $connectionOptions = array('pdo' => $this->getPdoConnection(), 'dbname' => $this->db->getName()); $evm = new \Doctrine\Common\EventManager(); $chainDriverImpl = new \Doctrine\ORM\Mapping\Driver\DriverChain(); $driverImpl = new \Cx\Core\Model\Controller\YamlDriver(array(ASCMS_CORE_PATH . '/Core' . '/Model/Yaml')); $chainDriverImpl->addDriver($driverImpl, 'Cx'); //loggable stuff $loggableDriverImpl = $config->newDefaultAnnotationDriver(ASCMS_LIBRARY_PATH . '/doctrine/Gedmo/Loggable/Entity'); $chainDriverImpl->addDriver($loggableDriverImpl, 'Gedmo\\Loggable'); $this->loggableListener = new \Cx\Core\Model\Model\Event\LoggableListener(); $this->loggableListener->setUsername('currently_loggedin_user'); // in real world app the username should be loaded from session, example: // Session::getInstance()->read('user')->getUsername(); $evm->addEventSubscriber($this->loggableListener); //tree stuff $treeListener = new \Gedmo\Tree\TreeListener(); $evm->addEventSubscriber($treeListener); $config->setMetadataDriverImpl($chainDriverImpl); //table prefix $prefixListener = new \DoctrineExtension\TablePrefixListener($this->db->getTablePrefix()); $evm->addEventListener(\Doctrine\ORM\Events::loadClassMetadata, $prefixListener); $config->setSqlLogger(new \Cx\Lib\DBG\DoctrineSQLLogger()); $em = \Cx\Core\Model\Controller\EntityManager::create($connectionOptions, $config, $evm); //resolve enum, set errors $conn = $em->getConnection(); $conn->setCharset($this->db->getCharset()); $conn->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string'); $conn->getDatabasePlatform()->registerDoctrineTypeMapping('set', 'string'); $this->em = $em; return $this->em; }