em.php 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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' => 'localhost',
  10. 'port' => 3306,
  11. 'user' => 'root',
  12. 'password' => 'nimda',
  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\ORM\Mapping\Driver\DriverChain();
  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 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. //$evm->addEventSubscriber($loggableListener);
  81. // timestampable
  82. $timestampableListener = new Gedmo\Timestampable\TimestampableListener;
  83. $timestampableListener->setAnnotationReader($cachedAnnotationReader);
  84. $evm->addEventSubscriber($timestampableListener);
  85. // blameable
  86. $blameableListener = new \Gedmo\Blameable\BlameableListener();
  87. $blameableListener->setAnnotationReader($cachedAnnotationReader);
  88. $blameableListener->setUserValue('MyUsername'); // determine from your environment
  89. $evm->addEventSubscriber($blameableListener);
  90. // translatable
  91. $translatableListener = new Gedmo\Translatable\TranslatableListener;
  92. // current translation locale should be set from session or hook later into the listener
  93. // most important, before entity manager is flushed
  94. $translatableListener->setTranslatableLocale('en');
  95. $translatableListener->setDefaultLocale('en');
  96. $translatableListener->setAnnotationReader($cachedAnnotationReader);
  97. $evm->addEventSubscriber($translatableListener);
  98. // sortable, not used in example
  99. //$sortableListener = new Gedmo\Sortable\SortableListener;
  100. //$sortableListener->setAnnotationReader($cachedAnnotationReader);
  101. //$evm->addEventSubscriber($sortableListener);
  102. // mysql set names UTF-8 if required
  103. $evm->addEventSubscriber(new Doctrine\DBAL\Event\Listeners\MysqlSessionInit());
  104. // Finally, create entity manager
  105. return Doctrine\ORM\EntityManager::create($connection, $config, $evm);