/** * Get entity manager. * * @return EntityManagerInterface */ protected function getEntityManager() { if (null === $this->entityManager) { $params = ['driver' => 'pdo_sqlite', 'memory' => true]; $cache = new ArrayCache(); /** @var AnnotationReader $reader */ $reader = new CachedReader(new AnnotationReader(), $cache); $annotationDriver = new AnnotationDriver($reader, [__DIR__ . '/../../../src/ORM']); $driverChain = new MappingDriverChain(); $driverChain->addDriver($annotationDriver, Commander::ENTITY_NAMESPACE); DoctrineExtensions::registerAbstractMappingIntoDriverChainORM($driverChain, $reader); $config = new Configuration(); $config->setAutoGenerateProxyClasses(true); $config->setProxyDir(sys_get_temp_dir()); $config->setProxyNamespace(Commander::ENTITY_NAMESPACE); $config->setMetadataDriverImpl($driverChain); $config->setMetadataCacheImpl($cache); $config->setQueryCacheImpl($cache); $config->setResultCacheImpl($cache); $config->setHydrationCacheImpl($cache); $timestampableListener = new TimestampableListener(); $timestampableListener->setAnnotationReader($annotationDriver->getReader()); $eventManager = new EventManager(); $eventManager->addEventSubscriber($timestampableListener); $entityManager = EntityManager::create($params, $config, $eventManager); $schemaTool = new SchemaTool($entityManager); $schemaTool->createSchema($entityManager->getMetadataFactory()->getAllMetadata()); $this->entityManager = $entityManager; } return $this->entityManager; }
public function register(Container $c) { $c['doctrine_extensions.timestampable.listener'] = function ($c) { $listener = new TimestampableListener(); $listener->setAnnotationReader($c['annotation.reader']); return $listener; }; }
public function __construct() { // load database configuration from CodeIgniter require APPPATH . 'config/database.php'; //A Doctrine Autoloader is needed to load the models // first argument of classloader is namespace and second argument is path // setup models/entity namespace $entityLoader = new ClassLoader('models', APPPATH); $entityLoader->register(); foreach (glob(APPPATH . 'modules/*', GLOB_ONLYDIR) as $m) { $module = str_replace(APPPATH . 'modules/', '', $m); $entityLoader = new ClassLoader($module, APPPATH . 'modules'); $entityLoader->register(); } //Register proxies namespace $proxyLoader = new ClassLoader('Proxies', APPPATH . 'Proxies'); $proxyLoader->register(); // Set up caches $config = new Configuration(); $cache = new ArrayCache(); $config->setMetadataCacheImpl($cache); $driverImpl = $config->newDefaultAnnotationDriver(array(APPPATH . 'models')); $config->setMetadataDriverImpl($driverImpl); $config->setQueryCacheImpl($cache); // Set up entity $reader = new AnnotationReader($cache); $models = array(APPPATH . 'models'); foreach (glob(APPPATH . 'modules/*/models', GLOB_ONLYDIR) as $m) { array_push($models, $m); } $driver = new AnnotationDriver($reader, $models); $config->setMetadataDriverImpl($driver); // Setup Gedmo $cachedAnnotationReader = new Doctrine\Common\Annotations\CachedReader($reader, $cache); // create a driver chain for metadata reading $driverChain = new Doctrine\ORM\Mapping\Driver\DriverChain(); // load superclass metadata mapping only, into driver chain // also registers Gedmo annotations.NOTE: you can personalize it Gedmo\DoctrineExtensions::registerAbstractMappingIntoDriverChainORM($driverChain, $cachedAnnotationReader); $event = new EventManager(); $timestampableListener = new TimestampableListener(); $timestampableListener->setAnnotationReader($cachedAnnotationReader); $event->addEventSubscriber($timestampableListener); $slugListener = new SluggableListener(); $slugListener->setAnnotationReader($cachedAnnotationReader); $event->addEventSubscriber($slugListener); // Proxy configuration $config->setProxyDir(APPPATH . '/proxies'); $config->setProxyNamespace('Proxies'); // Set up logger // $logger = new EchoSQLLogger; // $config->setSQLLogger($logger); $config->setAutoGenerateProxyClasses(TRUE); // Database connection information $connectionOptions = array('driver' => 'pdo_mysql', 'user' => $db['default']['username'], 'password' => $db['default']['password'], 'host' => $db['default']['hostname'], 'dbname' => $db['default']['database']); // Create EntityManager $this->em = EntityManager::create($connectionOptions, $config, $event); }
public function testTimestampableMetadata() { $meta = $this->em->getClassMetadata('Mapping\\Fixture\\Xml\\Timestampable'); $config = $this->timestampable->getConfiguration($this->em, $meta->name); $this->assertArrayHasKey('create', $config); $this->assertEquals('created', $config['create'][0]); $this->assertArrayHasKey('update', $config); $this->assertEquals('updated', $config['update'][0]); $this->assertArrayHasKey('change', $config); $onChange = $config['change'][0]; $this->assertEquals('published', $onChange['field']); $this->assertEquals('status.title', $onChange['trackedField']); $this->assertEquals('Published', $onChange['value']); }
$loader->loadClass($class); return class_exists($class, false); }); AnnotationRegistry::registerFile(__DIR__ . '/../vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php'); // globally used cache driver, in production use APC or memcached $cache = new Doctrine\Common\Cache\ArrayCache(); // standard annotation reader $annotationReader = new Doctrine\Common\Annotations\AnnotationReader(); $cachedAnnotationReader = new Doctrine\Common\Annotations\CachedReader($annotationReader, $cache); // create a driver chain for metadata reading $driverChain = new Doctrine\ORM\Mapping\Driver\DriverChain(); // load superclass metadata mapping only, into driver chain // also registers Gedmo annotations.NOTE: you can personalize it Gedmo\DoctrineExtensions::registerAbstractMappingIntoDriverChainORM($driverChain, $cachedAnnotationReader); //-- timestampable Listerner $timestampableListener = new TimestampableListener(); $timestampableListener->setAnnotationReader($cachedAnnotationReader); $app['doctrine.dbal.event_manager']->addEventSubscriber(new TimestampableListener()); }); //-- error $app->error(function (\Exception $e, $code) use($app) { if ($app['debug']) { return; } return $app['twig']->render('default/error.html.twig', array('code' => $code)); }); if ('preprod' == $app['env']) { //-- hack for PP(xpfabric) Request::trustProxyData(); } return $app;
/** * @param EventManager $manager * @param EntityManagerInterface $em * @param Reader|null $reader */ public function addSubscribers(EventManager $manager, EntityManagerInterface $em, Reader $reader = null) { $subscriber = new TimestampableListener(); $subscriber->setAnnotationReader($reader); $manager->addEventSubscriber($subscriber); }
/** * * @return EntityManager */ public function getEntityManager() { $cache = new DoctrineCache\ArrayCache(); $annotationReader = new AnnotationReader(); $cachedAnnotationReader = new CachedReader($annotationReader, $cache); // create a driver chain for metadata reading $driverChain = new MappingDriverChain(); // load superclass metadata mapping only, into driver chain // also registers Gedmo annotations.NOTE: you can personalize it Gedmo\DoctrineExtensions::registerAbstractMappingIntoDriverChainORM($driverChain, $cachedAnnotationReader); // now we want to register our application entities, // for that we need another metadata driver used for Entity namespace $annotationDriver = new AnnotationDriver($cachedAnnotationReader, $this->paths); $driverChain->addDriver($annotationDriver, $this->namespace); // general ORM configuration $isDevMode = $this->env != "production"; $config = DoctrineSetup::createAnnotationMetadataConfiguration($this->paths, $isDevMode); $config->setMetadataCacheImpl($cache); $config->setQueryCacheImpl($cache); $config->setMetadataDriverImpl($driverChain); $config->setProxyDir($this->proxy_path); $config->setProxyNamespace($this->namespace . '\\Proxy'); $config->setAutoGenerateProxyClasses($isDevMode); // Third, create event manager and hook prefered extension listeners $evm = new EventManager(); // gedmo extension listeners // 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); // tree $treeListener = new Gedmo\Tree\TreeListener(); $treeListener->setAnnotationReader($cachedAnnotationReader); $evm->addEventSubscriber($treeListener); // loggable, not used in example $loggableListener = new Gedmo\Loggable\LoggableListener(); $loggableListener->setAnnotationReader($cachedAnnotationReader); $loggableListener->setUsername('unknown'); $evm->addEventSubscriber($loggableListener); // timestampable $timestampableListener = new Gedmo\Timestampable\TimestampableListener(); $timestampableListener->setAnnotationReader($cachedAnnotationReader); $evm->addEventSubscriber($timestampableListener); // blameable $blameableListener = new Gedmo\Blameable\BlameableListener(); $blameableListener->setAnnotationReader($cachedAnnotationReader); $blameableListener->setUserValue('unknown'); // determine from your environment $evm->addEventSubscriber($blameableListener); // translatable - buggy !!! /* $translatableListener = new Gedmo\Translatable\TranslatableListener(); // current translation locale should be set from session or hook later into the listener // most important, before entity manager is flushed $translatableListener->setTranslatableLocale('en'); $translatableListener->setDefaultLocale('en'); $translatableListener->setAnnotationReader($cachedAnnotationReader); $evm->addEventSubscriber($translatableListener); */ // sortable, not used in example $sortableListener = new Gedmo\Sortable\SortableListener(); $sortableListener->setAnnotationReader($cachedAnnotationReader); $evm->addEventSubscriber($sortableListener); // mysql set names UTF-8 if required $evm->addEventSubscriber(new \Doctrine\DBAL\Event\Listeners\MysqlSessionInit()); // Finally, create entity manager return EntityManager::create($this->dbParams, $config, $evm); }
/** * Get event manager. * * @return EventManager */ public function getEventManager() { if (null === $this->eventManager) { $metadataDriver = $this->getEntityManagerConfig()->getMetadataDriverImpl(); $timestampableListener = new TimestampableListener(); if ($metadataDriver instanceof AnnotationDriver) { $timestampableListener->setAnnotationReader($metadataDriver->getReader()); } $eventManager = new EventManager(); $eventManager->addEventSubscriber($timestampableListener); $this->eventManager = $eventManager; } return $this->eventManager; }