Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.44 KB | None | 0 0
  1. protected function _initDoctrine() {
  2.  
  3. require_once('Doctrine/Common/ClassLoader.php');
  4. $autoloader = Zend_Loader_Autoloader::getInstance();
  5.  
  6. $classLoader = array(new DoctrineCommonClassLoader('Doctrine'), 'loadClass');
  7. $autoloader->pushAutoloader($classLoader, 'Doctrine\');
  8.  
  9. $classLoader = new DoctrineCommonClassLoader('Entities',
  10. realpath(Zend_Registry::get('config')->resources->entityManager->connection->entities), 'loadClass');
  11. $autoloader->pushAutoloader(array($classLoader, 'loadClass'), 'Entities');
  12.  
  13. $classLoader = new DoctrineCommonClassLoader('Repositories',
  14. realpath(Zend_Registry::get('config')->resources->entityManager->connection->entities), 'loadClass');
  15. $autoloader->pushAutoloader(array($classLoader, 'loadClass'), 'Repositories');
  16. }
  17.  
  18. class My_Resource_Entitymanager extends Zend_Application_Resource_ResourceAbstract
  19. {
  20.  
  21. public function init()
  22. {
  23.  
  24. // WARNING: setup, assumes that autoloaders are set
  25. // configuration settings from the application.ini file
  26. $zendConfig = new Zend_Config($this->getOptions());
  27. // globally used cache driver, in production use APC or memcached
  28. $cache = new DoctrineCommonCacheArrayCache;
  29. // standard annotation reader
  30. $annotationReader = new DoctrineCommonAnnotationsAnnotationReader;
  31. $cachedAnnotationReader = new DoctrineCommonAnnotationsCachedReader(
  32. $annotationReader, // use reader
  33. $cache // and a cache driver
  34. );
  35. // create a driver chain for metadata reading
  36. $driverChain = new DoctrineORMMappingDriverDriverChain();
  37. // load superclass metadata mapping only, into driver chain
  38. // also registers Gedmo annotations.NOTE: you can personalize it
  39. GedmoDoctrineExtensions::registerAbstractMappingIntoDriverChainORM(
  40. $driverChain, // our metadata driver chain, to hook into
  41. $cachedAnnotationReader // our cached annotation reader
  42. );
  43.  
  44. // now we want to register our application entities,
  45. // for that we need another metadata driver used for Entity namespace
  46. $annotationDriver = new DoctrineORMMappingDriverAnnotationDriver(
  47. $cachedAnnotationReader, // our cached annotation reader
  48. $zendConfig->connection->entities // paths to look in
  49. );
  50. // NOTE: driver for application Entity can be different, Yaml, Xml or whatever
  51. // register annotation driver for our application Entity namespace
  52. $driverChain->addDriver($annotationDriver, 'Entities');
  53.  
  54. // general ORM configuration
  55. $config = new DoctrineORMConfiguration;
  56. $config->setProxyDir($zendConfig->connection->proxies->location);
  57. $config->setProxyNamespace($zendConfig->connection->proxies->ns);
  58. $config->setAutoGenerateProxyClasses($zendConfig->connection->proxies->generate); // this can be based on production config.
  59. // register metadata driver
  60. $config->setMetadataDriverImpl($driverChain);
  61. // use our allready initialized cache driver
  62. $config->setMetadataCacheImpl($cache);
  63. $config->setQueryCacheImpl($cache);
  64.  
  65. // create event manager and hook prefered extension listeners
  66. $evm = new DoctrineCommonEventManager();
  67. // gedmo extension listeners, remove which are not used
  68.  
  69. // sluggable
  70. $sluggableListener = new GedmoSluggableSluggableListener;
  71. // you should set the used annotation reader to listener, to avoid creating new one for mapping drivers
  72. $sluggableListener->setAnnotationReader($cachedAnnotationReader);
  73. $evm->addEventSubscriber($sluggableListener);
  74.  
  75. // tree
  76. $treeListener = new GedmoTreeTreeListener;
  77. $treeListener->setAnnotationReader($cachedAnnotationReader);
  78. $evm->addEventSubscriber($treeListener);
  79.  
  80. // loggable, not used in example
  81. $loggableListener = new GedmoLoggableLoggableListener;
  82. $loggableListener->setAnnotationReader($cachedAnnotationReader);
  83. $evm->addEventSubscriber($loggableListener);
  84.  
  85. // timestampable
  86. $timestampableListener = new GedmoTimestampableTimestampableListener;
  87. $timestampableListener->setAnnotationReader($cachedAnnotationReader);
  88. $evm->addEventSubscriber($timestampableListener);
  89.  
  90. // translatable
  91. $translatableListener = new GedmoTranslatableTranslatableListener;
  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.  
  99. // sortable, not used in example
  100. $sortableListener = new GedmoSortableSortableListener;
  101. $sortableListener->setAnnotationReader($cachedAnnotationReader);
  102. $evm->addEventSubscriber($sortableListener);
  103.  
  104. // mysql set names UTF-8 if required
  105. $evm->addEventSubscriber(new DoctrineDBALEventListenersMysqlSessionInit());
  106. // DBAL connection
  107. $connection = array(
  108. 'driver' => "{$zendConfig->connection->driver}",
  109. 'host' => "{$zendConfig->connection->host}",
  110. 'dbname' => "{$zendConfig->connection->dbname}",
  111. 'user' => "{$zendConfig->connection->user}",
  112. 'password' => "{$zendConfig->connection->password}"
  113. );
  114. // Finally, create entity manager
  115. $em = DoctrineORMEntityManager::create($connection, $config, $evm);
  116. Zend_Registry::set('em', $em);
  117. return $em;
  118.  
  119. }
  120.  
  121. }
  122.  
  123. namespace Entities;
  124.  
  125. use DoctrineORMMapping as ORM;
  126. /**
  127. * @ORMEntity
  128. * @ORMHasLifecycleCallbacks
  129. */
  130. class USER_User
  131. {
  132. /**
  133. * @ORMId
  134. * @ORMColumn(type="integer")
  135. * @ORMGeneratedValue(strategy="AUTO")
  136. */
  137. private $id;
  138. /**
  139. * Retrieve user id
  140. */
  141. public function getId()
  142. {
  143. return $this->id;
  144. }
  145. }
  146.  
  147. ...
  148. includePaths.library = APPLICATION_PATH "/../library/Doctrine"
  149. ...
  150. pluginPaths.My_Resource = "My/Resource"
  151. ...
  152. autoloaderNamespaces[] = "Doctrine"
  153. autoloaderNamespaces[] = "Gedmo"
  154. autoloaderNamespaces[] = "Symfony"
  155. autoloaderNamespaces[] = "My"
  156. ...
  157. resources.entityManager.connection.driver = "pdo_mysql"
  158. resources.entityManager.connection.host = "localhost"
  159. resources.entityManager.connection.dbname = "test"
  160. resources.entityManager.connection.user = "test"
  161. resources.entityManager.connection.password = "test"
  162. resources.entityManager.connection.entities = APPLICATION_PATH "/models"
  163. resources.entityManager.connection.proxies.location = APPLICATION_PATH "/models/Proxies"
  164. resources.entityManager.connection.proxies.ns = "Proxies"
  165.  
  166. ; According to Doctrine manual, this should be true for
  167. ; development, and false for production
  168. resources.entityManager.connection.proxies.generate = true
  169. ...
  170.  
  171. use DoctrineCommonAnnotationsAnnotationRegistry;
  172. class My_Resource_Entitymanager extends Zend_Application_Resource_ResourceAbstract
  173. {
  174.  
  175. public function init()
  176. {
  177. AnnotationRegistry::registerFile(__DIR__ . '/../../Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php');
  178. // WARNING: setup, assumes that autoloaders are set
  179. // configuration settings from the application.ini file
  180. ...
  181.  
  182. $classLoader = array(new DoctrineCommonClassLoader('Doctrine'), 'loadClass');
  183. $autoloader->pushAutoloader($classLoader, 'Doctrine\');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement