/** * Factory method which creates an EntityManager. * * @return \Doctrine\ORM\EntityManager */ public function create() { $config = new \Doctrine\ORM\Configuration(); $config->setClassMetadataFactoryName('TYPO3\\Flow\\Persistence\\Doctrine\\Mapping\\ClassMetadataFactory'); $cache = new \TYPO3\Flow\Persistence\Doctrine\CacheAdapter(); // must use ObjectManager in compile phase... $cache->setCache($this->objectManager->get('TYPO3\\Flow\\Cache\\CacheManager')->getCache('Flow_Persistence_Doctrine')); $config->setMetadataCacheImpl($cache); $config->setQueryCacheImpl($cache); $resultCache = new \TYPO3\Flow\Persistence\Doctrine\CacheAdapter(); // must use ObjectManager in compile phase... $resultCache->setCache($this->objectManager->get('TYPO3\\Flow\\Cache\\CacheManager')->getCache('Flow_Persistence_Doctrine_Results')); $config->setResultCacheImpl($resultCache); if (class_exists($this->settings['doctrine']['sqlLogger'])) { $config->setSQLLogger(new $this->settings['doctrine']['sqlLogger']()); } $eventManager = $this->buildEventManager(); $flowAnnotationDriver = $this->objectManager->get('TYPO3\\Flow\\Persistence\\Doctrine\\Mapping\\Driver\\FlowAnnotationDriver'); $config->setMetadataDriverImpl($flowAnnotationDriver); $proxyDirectory = \TYPO3\Flow\Utility\Files::concatenatePaths(array($this->environment->getPathToTemporaryDirectory(), 'Doctrine/Proxies')); \TYPO3\Flow\Utility\Files::createDirectoryRecursively($proxyDirectory); $config->setProxyDir($proxyDirectory); $config->setProxyNamespace('TYPO3\\Flow\\Persistence\\Doctrine\\Proxies'); $config->setAutoGenerateProxyClasses(FALSE); $entityManager = \Doctrine\ORM\EntityManager::create($this->settings['backendOptions'], $config, $eventManager); $flowAnnotationDriver->setEntityManager($entityManager); \Doctrine\DBAL\Types\Type::addType('objectarray', 'TYPO3\\Flow\\Persistence\\Doctrine\\DataTypes\\ObjectArray'); if (isset($this->settings['doctrine']['filters']) && is_array($this->settings['doctrine']['filters'])) { foreach ($this->settings['doctrine']['filters'] as $filterName => $filterClass) { $config->addFilter($filterName, $filterClass); $entityManager->getFilters()->enable($filterName); } } return $entityManager; }
protected static function getEntityManager($options) { $config = new \Doctrine\ORM\Configuration(); // Handling for class names specified as platform types. if ($options['conn']['platform']) { $class_obj = new \ReflectionClass($options['conn']['platform']); $options['conn']['platform'] = $class_obj->newInstance(); } // Special handling for the utf8mb4 type. if ($options['conn']['driver'] == 'pdo_mysql' && $options['conn']['charset'] == 'utf8mb4') { $options['conn']['platform'] = new \DF\Doctrine\Platform\MysqlUnicode(); } $metadata_driver = $config->newDefaultAnnotationDriver($options['modelPath']); $config->setMetadataDriverImpl($metadata_driver); $cache = new \DF\Doctrine\Cache(); // $cache->setNamespace('doctrine_'); $config->setMetadataCacheImpl($cache); $config->setQueryCacheImpl($cache); $config->setResultCacheImpl($cache); $config->setProxyDir($options['proxyPath']); $config->setProxyNamespace($options['proxyNamespace']); if (isset($options['conn']['debug']) && $options['conn']['debug']) { $config->setSQLLogger(new \DF\Doctrine\Logger\EchoSQL()); } $config->addFilter('softdelete', '\\DF\\Doctrine\\Filter\\SoftDelete'); $config->addCustomNumericFunction('RAND', '\\DF\\Doctrine\\Functions\\Rand'); $evm = new \Doctrine\Common\EventManager(); $em = \Doctrine\ORM\EntityManager::create($options['conn'], $config, $evm); $em->getFilters()->enable("softdelete"); // Try the connection before rendering the page. $em->getConnection()->connect(); return $em; }
public function setUp() { $reader = new \Doctrine\Common\Annotations\AnnotationReader(); $driver = new \Doctrine\ORM\Mapping\Driver\AnnotationDriver($reader); $config = new \Doctrine\ORM\Configuration(); $config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache()); $config->setQueryCacheImpl(new \Doctrine\Common\Cache\ArrayCache()); $config->setProxyDir(sys_get_temp_dir()); $config->setProxyNamespace('SimpleThings\\EntityAudit\\Tests\\Proxies'); $config->setMetadataDriverImpl($driver); $conn = array('driver' => $GLOBALS['DOCTRINE_DRIVER'], 'memory' => $GLOBALS['DOCTRINE_MEMORY'], 'dbname' => $GLOBALS['DOCTRINE_DATABASE'], 'user' => $GLOBALS['DOCTRINE_USER'], 'password' => $GLOBALS['DOCTRINE_PASSWORD'], 'host' => $GLOBALS['DOCTRINE_HOST']); $auditConfig = new AuditConfiguration(); $auditConfig->setCurrentUsername("beberlei"); $auditConfig->setAuditedEntityClasses($this->auditedEntities); $auditConfig->setGlobalIgnoreColumns(array('ignoreme')); $this->auditManager = new AuditManager($auditConfig); $this->auditManager->registerEvents($evm = new EventManager()); if (php_sapi_name() == 'cli' && isset($_SERVER['argv']) && (in_array('-v', $_SERVER['argv']) || in_array('--verbose', $_SERVER['argv']))) { $config->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger()); } $this->em = \Doctrine\ORM\EntityManager::create($conn, $config, $evm); $schemaTool = new \Doctrine\ORM\Tools\SchemaTool($this->em); $em = $this->em; try { $schemaTool->createSchema(array_map(function ($value) use($em) { return $em->getClassMetadata($value); }, $this->schemaEntities)); } catch (\Exception $e) { if ($GLOBALS['DOCTRINE_DRIVER'] != 'pdo_mysql' || !($e instanceof \PDOException && strpos($e->getMessage(), 'Base table or view already exists') !== false)) { throw $e; } } }
protected function createEntityManager($conn) { $config = new \Doctrine\ORM\Configuration(); $config->setProxyDir(__DIR__ . '/../../../Proxies'); $config->setProxyNamespace('MyProject\\Proxies'); $config->setAutoGenerateProxyClasses(true); $annotDriver = $config->newDefaultAnnotationDriver(array(__DIR__ . '/../../../Models/')); $config->setMetadataDriverImpl($annotDriver); $cache = new \Doctrine\Common\Cache\ArrayCache(); $config->setMetadataCacheImpl($cache); $config->setQueryCacheImpl($cache); $config->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger()); $em = \Doctrine\ORM\EntityManager::create($conn, $config); return $em; }
/** * Setup system, not included in benchmark. */ public function setUp() { require_once $this->rootPath . "/lib/Doctrine/ORM/Version.php"; if (version_compare(\Doctrine\ORM\Version::VERSION, '2.3.99') > 0) { require_once $this->rootPath . "/vendor/autoload.php"; $config = \Doctrine\ORM\Tools\Setup::createAnnotationMetadataConfiguration(array(__DIR__ . "/Orm")); } else { if (version_compare(\Doctrine\ORM\Version::VERSION, '2.2.99') > 0) { require_once $this->rootPath . "/lib/Doctrine/ORM/Tools/Setup.php"; \Doctrine\ORM\Tools\Setup::registerAutoloadGit($this->rootPath); $config = \Doctrine\ORM\Tools\Setup::createAnnotationMetadataConfiguration(array(__DIR__ . "/Orm")); } else { require_once $this->rootPath . "/lib/vendor/doctrine-common/lib/Doctrine/Common/ClassLoader.php"; $loader = new \Doctrine\Common\ClassLoader("Doctrine\\ORM", $this->rootPath . "/lib/"); $loader->register(); $loader = new \Doctrine\Common\ClassLoader("Doctrine\\DBAL", $this->rootPath . "/lib/vendor/doctrine-dbal/lib"); $loader->register(); $loader = new \Doctrine\Common\ClassLoader("Doctrine\\Common", $this->rootPath . "/lib/vendor/doctrine-common/lib"); $loader->register(); $config = new \Doctrine\ORM\Configuration(); $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver(__DIR__ . "/Orm")); } } $cache = new \Doctrine\Common\Cache\ArrayCache(); $this->queryCount = new DbalQueryCountLogger(); $config->setMetadataCacheImpl($cache); $config->setQueryCacheImpl($cache); // not sql query cache, but dql query parsing cache. $config->setProxyDir(__DIR__ . "/proxies"); $config->setProxyNamespace('Proxies'); $config->setAutoGenerateProxyClasses(false); $config->setSQLLogger($this->queryCount); $dbParams = array('driver' => 'pdo_sqlite', 'memory' => true); $this->em = \Doctrine\ORM\EntityManager::create($dbParams, $config); $classes = $this->em->getMetadataFactory()->getAllMetadata(); $schemaTool = new \Doctrine\ORM\Tools\SchemaTool($this->em); try { $schemaTool->dropSchema($classes); } catch (Exception $e) { echo $e->getMessage(); } $schemaTool->createSchema($classes); $this->em->getProxyFactory()->generateProxyClasses($classes, __DIR__ . '/proxies'); }
} foreach ($entitiesPath as $name => $path) { $classLoader = new \Doctrine\Common\ClassLoader($name, $path); $classLoader->register(); } // Setup Entity Manager // **************************************************************** $ormConfig = new \Doctrine\ORM\Configuration(); // custom zend form annotations $annoReader = new \Doctrine\Common\Annotations\AnnotationReader(); $annoReader->setAutoloadAnnotations(true); $annoReader->setDefaultAnnotationNamespace('Doctrine\\ORM\\Mapping\\'); $annoReader->setAnnotationNamespaceAlias('Awe\\Annotations\\', 'awe'); $annoDriver = new \Doctrine\ORM\Mapping\Driver\AnnotationDriver($annoReader, $entitiesPath); $ormConfig->setMetadataDriverImpl($annoDriver); // logging $ormConfig->setSQLLogger(new \Doctrine\DBAL\Logging\FileSQLLogger($logPath)); // caching if (is_array($cacheType)) { $ormConfig->setQueryCacheImpl(new $cacheType['query']()); $ormConfig->setMetadataCacheImpl(new $cacheType['metadata']()); } else { $ormConfig->setQueryCacheImpl(new $cacheType()); $ormConfig->setMetadataCacheImpl(new $cacheType()); } // proxies $ormConfig->setAutoGenerateProxyClasses(true); $ormConfig->setProxyDir($proxiesPath . '/Proxies'); $ormConfig->setProxyNamespace('Proxies'); // Start Doctrine $em = \Doctrine\ORM\EntityManager::create($dbLogins, $ormConfig);
/** * Factory method which creates an EntityManager. * * @return \Doctrine\ORM\EntityManager */ public function create() { $config = new \Doctrine\ORM\Configuration(); $config->setClassMetadataFactoryName('TYPO3\\FLOW3\\Persistence\\Doctrine\\Mapping\\ClassMetadataFactory'); if (class_exists($this->settings['doctrine']['cacheImplementation'])) { // safeguard against apc being disabled in CLI... if ($this->settings['doctrine']['cacheImplementation'] !== 'Doctrine\\Common\\Cache\\ApcCache' || function_exists('apc_fetch')) { $cache = new $this->settings['doctrine']['cacheImplementation'](); $config->setMetadataCacheImpl($cache); $config->setQueryCacheImpl($cache); } } if (class_exists($this->settings['doctrine']['sqlLogger'])) { $config->setSQLLogger(new $this->settings['doctrine']['sqlLogger']()); } // must use ObjectManager in compile phase... $flow3AnnotationDriver = $this->objectManager->get('TYPO3\\FLOW3\\Persistence\\Doctrine\\Mapping\\Driver\\Flow3AnnotationDriver'); $config->setMetadataDriverImpl($flow3AnnotationDriver); $proxyDirectory = \TYPO3\FLOW3\Utility\Files::concatenatePaths(array($this->environment->getPathToTemporaryDirectory(), 'Doctrine/Proxies')); \TYPO3\FLOW3\Utility\Files::createDirectoryRecursively($proxyDirectory); $config->setProxyDir($proxyDirectory); $config->setProxyNamespace('TYPO3\\FLOW3\\Persistence\\Doctrine\\Proxies'); $config->setAutoGenerateProxyClasses(FALSE); $entityManager = \Doctrine\ORM\EntityManager::create($this->settings['backendOptions'], $config); $flow3AnnotationDriver->setEntityManager($entityManager); return $entityManager; }
/** * @return $this */ public function init() { $that = $this; $dispatcher = $this->app->getDispatcher(); $monitor = $dispatcher->getMonitor(); // Configuration $this->app->getConfig()->load($this->getPath() . '/config/settings.php')->load($this->getPath() . '/config/' . $this->app->getEnvironment() . '/settings.php'); // Auth $this->app->single('core.auth', function () use($that) { $em = $that->getApp()->get('core.entity.manager'); $session = $that->getApp()->get('core.session'); $space = new Session\Space($session, 'core.auth'); $auth = new Acl($em, $space); $that->app->getConfig()->grab('auth')->inject($auth); return $auth; }); // Data cache $this->app->single('core.cache.data', function () use($that) { $config = $that->getApp()->getConfig()->grab('cache.data'); $cache = $that->getCache($config); return $cache; }); // Accelerator cache $this->app->single('core.cache.accelerator', function () use($that) { $config = $that->getApp()->getConfig()->grab('cache.accelerator'); $cache = $that->getCache($config); return $cache; }); // Entity manager $this->app->single('core.entity.manager', function () use($that) { $config = $that->getApp()->getConfig(); $dataCache = $that->getApp()->get('core.cache.data'); $acceleratorCache = $that->getApp()->get('core.cache.accelerator'); $cfg = new \Doctrine\ORM\Configuration(); $cfg->setResultCacheImpl($dataCache); $cfg->setMetadataCacheImpl($acceleratorCache); $cfg->setQueryCacheImpl($acceleratorCache); $cfg->setProxyDir($config->get('cache.proxyDir')); $cfg->setProxyNamespace('Proxies'); $cfg->setSQLLogger(new \Doctrine\DBAL\Logging\DebugStack()); $paths = $that->getApp()->findPaths('Entity'); $driverImpl = $cfg->newDefaultAnnotationDriver($paths); $cfg->setMetadataDriverImpl($driverImpl); $conn = \Doctrine\DBAL\DriverManager::getConnection($config->get('db')->getData(), $cfg, new \Doctrine\Common\EventManager()); $em = new Entity\Manager($that->getApp(), $conn, $cfg, $conn->getEventManager()); return $em; }); // Session $this->app->single('core.session', function () use($that) { $session = new Session(); $that->getApp()->getConfig()->grab('session')->inject($session); return $session; }); // Resource manager $this->app->single('core.resource.manager', function () use($that) { $rm = new Resource\Manager(); $that->getApp()->getConfig()->grab('resource.manager')->inject($rm); return $rm; }); // Asset manager $this->app->single('core.am', function () use($that) { $am = new Asset\Manager($that->getApp()); $that->getApp()->getConfig()->grab('asset.manager')->inject($am); return $am; }); // Browser console output $this->app->single('core.console', function () { $log = new \Monolog\Logger('console'); $log->pushHandler(new \Monolog\Handler\ChromePHPHandler()); $log->pushHandler(new \Monolog\Handler\FirePHPHandler()); return $log; }); // Request tracker $this->app->single('core.tracker', function () use($that) { $request = $that->getApp()->getRequest(); $session = $that->getApp()->get('core.session'); $space = new Session\Space($session, 'core.tracker'); $tracker = new Tracker($space, $request); $that->getApp()->getConfig()->grab('tracker')->inject($tracker); return $tracker; }); // Logger $this->app->single('core.log', function () use($that) { $config = $that->getApp()->getConfig(); $env = $that->getApp()->getEnvironment(); $log = new \Monolog\Logger('log'); $log->pushHandler(new \Monolog\Handler\StreamHandler($config->get('log.app'))); $log->pushProcessor(new \Monolog\Processor\WebProcessor()); $log->pushProcessor(new \Monolog\Processor\MemoryUsageProcessor()); $log->pushProcessor(new \Monolog\Processor\IntrospectionProcessor()); if (in_array($env, array(App::DEVELOPMENT))) { $log->pushHandler(new \Monolog\Handler\ChromePHPHandler()); $log->pushHandler(new \Monolog\Handler\FirePHPHandler()); } return $log; }); // Locale $this->app->set('core.locale', function () use($that) { $code = $that->getApp()->getContext()->getLocale()->getActive(); $locale = $that->getApp()->get('core.entity.manager')->getRepository('Cms\\Entity\\System\\Locale')->findOneByCode($code); return $locale; }); $this->app->set('core.locale.switcher', function () use($that) { $locale = $that->getApp()->getContext()->getLocale(); $session = $that->getApp()->get('core.session'); $space = new Session\Space($session, 'core.locale.switcher'); $switcher = new Switcher($locale, $space); $that->getApp()->getConfig()->grab('locale.switcher')->inject($switcher); return $switcher; }); // Dispatcher events $monitor->listen(Dispatcher::STARTED, function () use($that) { $switcher = $that->getApp()->get('core.locale.switcher'); $switcher->remember(); }); $monitor->listen(Dispatcher::ROUTE_FOUND, function () use($that) { $dispatcher = $that->getApp()->getDispatcher(); $route = $dispatcher->getRoute(); $auth = $that->getApp()->get('core.auth'); if (!$auth->isAccessible($route)) { $dispatcher->forceRoute('site-error-unauthorized'); } }); $monitor->listen(Dispatcher::ENDED, function () use($that) { $app = $that->getApp(); $tracker = $that->getApp()->get('core.tracker'); $tracker->track(); if ($that->getApp()->getEnvironment() == App::DEVELOPMENT) { $em = $app->get('core.entity.manager'); $console = $app->get('core.console'); $sql = $em->getConfiguration()->getSQLLogger(); foreach ($sql->queries as $query) { $data = array('params' => $query['params'], 'types' => $query['types']); $console->debug($query['sql'], $data); } } }); return $this; }
/** * Get Doctrine2 * * @param string $db The database instance to get (if using multiple databases) * @return Doctrine\ORM\EntityManager */ public function getDoctrine2($db = 'default') { if ($this->_doctrine2 === null || !isset($this->_doctrine2[$db])) { // Get Doctrine configuration options from the application.ini file $dconfig = $this->getOptions(); if ($db != 'default') { $dconfig = $dconfig[$db]; } try { if (Zend_Registry::isRegistered('d2cache')) { $cache = Zend_Registry::get('d2cache'); } else { $d2cacheOptions = $this->getBootstrap()->getApplication()->getOptions()['resources']['doctrine2cache']; if (!$d2cacheOptions || !isset($d2cacheOptions['type'])) { throw new Zend_Exception('force err'); } $plugin = new OSS_Resource_Doctrine2cache($d2cacheOptions); $this->getBootstrap()->registerPluginResource($plugin); $cache = $plugin->getDoctrine2cache(); } } catch (Zend_Exception $e) { die(_('ERROR: Doctrine2 requires Doctrine2Cache to have been already bootstrapped')); } $config = new Doctrine\ORM\Configuration(); $config->setMetadataCacheImpl($cache); $driver = new \Doctrine\ORM\Mapping\Driver\XmlDriver(array($dconfig['xml_schema_path'])); $config->setMetadataDriverImpl($driver); $config->setQueryCacheImpl($cache); $config->setResultCacheImpl($cache); $config->setProxyDir($dconfig['proxies_path']); $config->setProxyNamespace($dconfig['proxies_namespace']); $config->setAutoGenerateProxyClasses($dconfig['autogen_proxies']); if (isset($dconfig['logger']) && $dconfig['logger']) { $config->setSQLLogger(new OSS_Doctrine2_FirebugProfiler()); } $this->_doctrine2[$db] = Doctrine\ORM\EntityManager::create($dconfig['connection']['options'], $config); $modelAutoLoader = new \Doctrine\Common\ClassLoader($dconfig['models_namespace'], realpath($dconfig['models_path'])); $repositoryAutoLoader = new \Doctrine\Common\ClassLoader($dconfig['repositories_namespace'], realpath($dconfig['repositories_path'])); $autoloader = Zend_Loader_Autoloader::getInstance(); $autoloader->pushAutoloader(array($modelAutoLoader, 'loadClass'), $dconfig['models_namespace']); $autoloader->pushAutoloader(array($repositoryAutoLoader, 'loadClass'), $dconfig['repositories_namespace']); // http://docs.doctrine-project.org/en/latest/reference/configuration.html#autoloading-proxies Doctrine\ORM\Proxy\Autoloader::register($dconfig['proxies_path'], $dconfig['proxies_namespace']); } return $this->_doctrine2[$db]; }
/** * @param \Nette\DI\Container * @return \Doctrine\ORM\Configuration */ public static function createServiceConfiguration(DI\Container $context) { $config = new \Doctrine\ORM\Configuration; // Cache $storage = $context->hasService('metadataCache') ? $context->metadataCache : new Cache($context->cacheStorage); $config->setMetadataCacheImpl($storage); $storage = $context->hasService('queryCache') ? $context->queryCache : new Cache($context->cacheStorage); $config->setQueryCacheImpl($storage); // Metadata $config->setClassMetadataFactoryName('Nella\Doctrine\Mapping\ClassMetadataFactory'); $config->setMetadataDriverImpl($context->annotationDriver); // Proxies $config->setProxyDir($this->configuration['proxyDir']); $config->setProxyNamespace($this->configuration['proxyNamespace']); if ($this->configuration['productionMode']) { $config->setAutoGenerateProxyClasses(FALSE); } else { if ($context->hasService('logger')) { $config->setSQLLogger($context->logger); } $config->setAutoGenerateProxyClasses(TRUE); } return $config; }
$classLoader = new \Doctrine\Common\ClassLoader($name); } $classLoader->register(); } foreach ($entitiesPath as $name => $path) { $classLoader = new \Doctrine\Common\ClassLoader($name, $path); $classLoader->register(); } // Setup Entity Manager // **************************************************************** $ormConfig = new \Doctrine\ORM\Configuration(); // custom zend form annotations $driverImpl = $ormConfig->newDefaultAnnotationDriver($entitiesPath); $ormConfig->setMetadataDriverImpl($driverImpl); // logging //$ormConfig->setSQLLogger(new \Doctrine\DBAL\Logging\FileSQLLogger($logPath)); $ormConfig->setSQLLogger(new \Doctrine\DBAL\Logging\DebugStack()); // caching if (is_array($cacheType)) { $ormConfig->setQueryCacheImpl(new $cacheType['query']()); $ormConfig->setMetadataCacheImpl(new $cacheType['metadata']()); } else { $ormConfig->setQueryCacheImpl(new $cacheType()); $ormConfig->setMetadataCacheImpl(new $cacheType()); } // proxies $ormConfig->setAutoGenerateProxyClasses(true); $ormConfig->setProxyDir($proxiesPath . '/Proxies'); $ormConfig->setProxyNamespace('Proxies'); // Start Doctrine $em = \Doctrine\ORM\EntityManager::create($dbLogins, $ormConfig);