em.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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' => 'nimda',
  13. 'dbname' => 'test',
  14. 'driver' => 'pdo_mysql'
  15. );
  16. // First of all autoloading of vendors
  17. $vendorPath = realpath(__DIR__.'/../vendor');
  18. $gedmoPath = realpath(__DIR__.'/../lib');
  19. $doctrineClassLoaderFile = $vendorPath.'/doctrine-common/lib/Doctrine/Common/ClassLoader.php';
  20. if (!file_exists($doctrineClassLoaderFile)) {
  21. die('cannot find vendor, run: php bin/vendors.php to install doctrine');
  22. }
  23. require $doctrineClassLoaderFile;
  24. use Doctrine\Common\ClassLoader;
  25. // autoload all vendors
  26. $loader = new ClassLoader('Doctrine\Common', $vendorPath.'/doctrine-common/lib');
  27. $loader->register();
  28. $loader = new ClassLoader('Doctrine\DBAL', $vendorPath.'/doctrine-dbal/lib');
  29. $loader->register();
  30. $loader = new ClassLoader('Doctrine\ORM', $vendorPath.'/doctrine-orm/lib');
  31. $loader->register();
  32. // gedmo extensions
  33. $loader = new ClassLoader('Gedmo', $gedmoPath);
  34. $loader->register();
  35. // if you use yaml, you need a yaml parser, same as command line tool
  36. $loader = new ClassLoader('Symfony', $vendorPath);
  37. $loader->register();
  38. // autoloader for Entity namespace
  39. $loader = new ClassLoader('Entity', __DIR__.'/app');
  40. $loader->register();
  41. // Second configure ORM
  42. $config = new Doctrine\ORM\Configuration;
  43. $config->setProxyDir(sys_get_temp_dir());
  44. $config->setProxyNamespace('Proxy');
  45. $config->setAutoGenerateProxyClasses(false);
  46. // standard annotation reader
  47. $annotationReader = new Doctrine\Common\Annotations\AnnotationReader;
  48. // gedmo annotation loader
  49. Doctrine\Common\Annotations\AnnotationRegistry::registerAutoloadNamespace(
  50. 'Gedmo\Mapping\Annotation',
  51. $gedmoPath
  52. );
  53. // standard doctrine annotations
  54. Doctrine\Common\Annotations\AnnotationRegistry::registerFile(
  55. $vendorPath.'/doctrine-orm/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php'
  56. );
  57. // register annotation driver
  58. $driverChain = new Doctrine\ORM\Mapping\Driver\DriverChain();
  59. $annotationDriver = new Doctrine\ORM\Mapping\Driver\AnnotationDriver($annotationReader, array(
  60. __DIR__.'/app/Entity', // example entity
  61. $gedmoPath.'/Gedmo/Translatable/Entity',
  62. $gedmoPath.'/Gedmo/Loggable/Entity',
  63. $gedmoPath.'/Gedmo/Tree/Entity',
  64. ));
  65. // drivers for diferent namespaces
  66. $driverChain->addDriver($annotationDriver, 'Entity');
  67. $driverChain->addDriver($annotationDriver, 'Gedmo\Translatable\Entity');
  68. $driverChain->addDriver($annotationDriver, 'Gedmo\Loggable\Entity');
  69. $driverChain->addDriver($annotationDriver, 'Gedmo\Tree\Entity');
  70. // register metadata driver
  71. $config->setMetadataDriverImpl($driverChain);
  72. // cache
  73. $config->setMetadataCacheImpl(new Doctrine\Common\Cache\ArrayCache);
  74. $config->setQueryCacheImpl(new Doctrine\Common\Cache\ArrayCache);
  75. $evm = new Doctrine\Common\EventManager();
  76. // gedmo extension listeners
  77. $evm->addEventSubscriber(new Gedmo\Sluggable\SluggableListener);
  78. $evm->addEventSubscriber(new Gedmo\Tree\TreeListener);
  79. $evm->addEventSubscriber(new Gedmo\Loggable\LoggableListener);
  80. $evm->addEventSubscriber(new Gedmo\Timestampable\TimestampableListener);
  81. $translatable = new Gedmo\Translatable\TranslationListener;
  82. $translatable->setTranslatableLocale('en');
  83. $translatable->setDefaultLocale('en');
  84. $evm->addEventSubscriber($translatable);
  85. // mysql set names UTF-8
  86. $evm->addEventSubscriber(new Doctrine\DBAL\Event\Listeners\MysqlSessionInit());
  87. // create entity manager
  88. return Doctrine\ORM\EntityManager::create($connection, $config, $evm);