em.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * This entity manager configuration works with doctrine 2.1.x and 2.2.x
  4. * versions. Regarding AnnotationDriver setup it most probably will be changed into
  5. * xml. Because annotation driver fails to read other classes in same namespace
  6. */
  7. // connection args, modify at will
  8. $connection = array(
  9. 'host' => '127.0.0.1',
  10. 'port' => 3306,
  11. 'user' => 'root',
  12. 'password' => null,
  13. 'dbname' => 'test',
  14. 'driver' => 'pdo_mysql',
  15. );
  16. if (!file_exists(__DIR__.'/../vendor/autoload.php')) {
  17. die('cannot find vendors, read README.md how to use composer');
  18. }
  19. // First of all autoloading of vendors
  20. $loader = require __DIR__.'/../vendor/autoload.php';
  21. // gedmo extensions
  22. $loader->add('Gedmo', __DIR__.'/../lib');
  23. // autoloader for Entity namespace
  24. $loader->add('Entity', __DIR__.'/app');
  25. // ensure standard doctrine annotations are registered
  26. Doctrine\Common\Annotations\AnnotationRegistry::registerFile(
  27. __DIR__.'/../vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php'
  28. );
  29. // Second configure ORM
  30. // globally used cache driver, in production use APC or memcached
  31. $cache = new Doctrine\Common\Cache\ArrayCache();
  32. // standard annotation reader
  33. $annotationReader = new Doctrine\Common\Annotations\AnnotationReader();
  34. $cachedAnnotationReader = new Doctrine\Common\Annotations\CachedReader(
  35. $annotationReader, // use reader
  36. $cache // and a cache driver
  37. );
  38. // create a driver chain for metadata reading
  39. $driverChain = new Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain();
  40. // load superclass metadata mapping only, into driver chain
  41. // also registers Gedmo annotations.NOTE: you can personalize it
  42. Gedmo\DoctrineExtensions::registerAbstractMappingIntoDriverChainORM(
  43. $driverChain, // our metadata driver chain, to hook into
  44. $cachedAnnotationReader // our cached annotation reader
  45. );
  46. // now we want to register our application entities,
  47. // for that we need another metadata driver used for Entity namespace
  48. $annotationDriver = new Doctrine\ORM\Mapping\Driver\AnnotationDriver(
  49. $cachedAnnotationReader, // our cached annotation reader
  50. array(__DIR__.'/app/Entity') // paths to look in
  51. );
  52. // NOTE: driver for application Entity can be different, Yaml, Xml or whatever
  53. // register annotation driver for our application Entity fully qualified namespace
  54. $driverChain->addDriver($annotationDriver, 'Entity');
  55. // general ORM configuration
  56. $config = new Doctrine\ORM\Configuration();
  57. $config->setProxyDir(sys_get_temp_dir());
  58. $config->setProxyNamespace('Proxy');
  59. $config->setAutoGenerateProxyClasses(false); // this can be based on production config.
  60. // register metadata driver
  61. $config->setMetadataDriverImpl($driverChain);
  62. // use our allready initialized cache driver
  63. $config->setMetadataCacheImpl($cache);
  64. $config->setQueryCacheImpl($cache);
  65. // Third, create event manager and hook prefered extension listeners
  66. $evm = new Doctrine\Common\EventManager();
  67. // gedmo extension listeners
  68. // sluggable
  69. $sluggableListener = new Gedmo\Sluggable\SluggableListener();
  70. // you should set the used annotation reader to listener, to avoid creating new one for mapping drivers
  71. $sluggableListener->setAnnotationReader($cachedAnnotationReader);
  72. $evm->addEventSubscriber($sluggableListener);
  73. // tree
  74. $treeListener = new Gedmo\Tree\TreeListener();
  75. $treeListener->setAnnotationReader($cachedAnnotationReader);
  76. $evm->addEventSubscriber($treeListener);
  77. // loggable, not used in example
  78. //$loggableListener = new Gedmo\Loggable\LoggableListener;
  79. //$loggableListener->setAnnotationReader($cachedAnnotationReader);
  80. //$loggableListener->setUsername('admin');
  81. //$evm->addEventSubscriber($loggableListener);
  82. // timestampable
  83. $timestampableListener = new Gedmo\Timestampable\TimestampableListener();
  84. $timestampableListener->setAnnotationReader($cachedAnnotationReader);
  85. $evm->addEventSubscriber($timestampableListener);
  86. // blameable
  87. $blameableListener = new \Gedmo\Blameable\BlameableListener();
  88. $blameableListener->setAnnotationReader($cachedAnnotationReader);
  89. $blameableListener->setUserValue('MyUsername'); // determine from your environment
  90. $evm->addEventSubscriber($blameableListener);
  91. // translatable
  92. $translatableListener = new Gedmo\Translatable\TranslatableListener();
  93. // current translation locale should be set from session or hook later into the listener
  94. // most important, before entity manager is flushed
  95. $translatableListener->setTranslatableLocale('en');
  96. $translatableListener->setDefaultLocale('en');
  97. $translatableListener->setAnnotationReader($cachedAnnotationReader);
  98. $evm->addEventSubscriber($translatableListener);
  99. // sortable, not used in example
  100. //$sortableListener = new Gedmo\Sortable\SortableListener;
  101. //$sortableListener->setAnnotationReader($cachedAnnotationReader);
  102. //$evm->addEventSubscriber($sortableListener);
  103. // mysql set names UTF-8 if required
  104. $evm->addEventSubscriber(new Doctrine\DBAL\Event\Listeners\MysqlSessionInit());
  105. // Finally, create entity manager
  106. return Doctrine\ORM\EntityManager::create($connection, $config, $evm);