Integrating Zend Framework 1.10 with Doctrine 2 (using Doctrine's Autoloader)


/ Published in: PHP
Save to your folder(s)

an alternative to a previous method i did using Zend Framework's autoloaders only. this method uses Doctrine's autoloaders for doctrine's classes and Zend's for Zend's :)

i think the difficulty i had was autoloading from a specific directory (with just Zend's autoloaders). eg. in Doctrine autoloaders i will do

new Doctrine\Common\ClassLoader(
'Application\Models',
APPLICATION_PATH . '/models'
);

where the 2nd param is the path to find the classes. basically,

- we create a resource method in `bootstrap.php`.
- in it, we setup doctrine in a similar way to how the doctrine sandbox sets it up. to setup autoloading of doctrine's classes, as we create new doctrine class loaders, we push them into the zend's autoloader.
- then its basically the same as the sandbox app, setting up caches, metadata drivers, proxies etc.


Copy this code and paste it in your HTML
  1. function _initDoctrine() {
  2. // setup Zend & Doctrine Autoloaders
  3. require_once "Doctrine/Common/ClassLoader.php";
  4.  
  5. $zendAutoloader = Zend_Loader_Autoloader::getInstance();
  6.  
  7. // $autoloader = array(new \Doctrine\Common\ClassLoader(), 'loadClass');
  8.  
  9. $autoloader = array(new \Doctrine\Common\ClassLoader('Symfony'), 'loadClass');
  10. $zendAutoloader->pushAutoloader($autoloader, 'Symfony\\');
  11. $autoloader = array(new \Doctrine\Common\ClassLoader('Doctrine'), 'loadClass');
  12. $zendAutoloader->pushAutoloader($autoloader, 'Doctrine\\');
  13. $autoloader = array(new \Doctrine\Common\ClassLoader('DoctrineExtensions'), 'loadClass');
  14. $zendAutoloader->pushAutoloader($autoloader, 'DoctrineExtensions\\');
  15. $autoloader = array(new \Doctrine\Common\ClassLoader('Application\\Models', realpath(__DIR__ . '/..')), 'loadClass');
  16. $zendAutoloader->pushAutoloader($autoloader, 'Application\\Models\\');
  17. $autoloader = array(new \Doctrine\Common\ClassLoader('Application\\Proxies', realpath(__DIR__ . '/..')), 'loadClass');
  18. $zendAutoloader->pushAutoloader($autoloader, 'Application\\Proxies');
  19. $autoloader = array(new \Doctrine\Common\ClassLoader('DoctrineExtensions'), 'loadClass');
  20. $zendAutoloader->pushAutoloader($autoloader, 'DoctrineExtensions\\');
  21.  
  22. // setup configuration as seen from the sandbox application
  23. // TODO: read configuration from application.ini
  24. $config = new \Doctrine\ORM\Configuration;
  25. $cache = new \Doctrine\Common\Cache\ArrayCache;
  26. $config->setMetadataCacheImpl($cache);
  27. $driverImpl = $config->newDefaultAnnotationDriver(realpath(__DIR__ . '/models'));
  28. $config->setMetadataDriverImpl($driverImpl);
  29. $config->setQueryCacheImpl($cache);
  30. $config->setProxyDir(realpath(__DIR__ . '/proxies'));
  31. $config->setProxyNamespace('Application\\Proxies');
  32. $config->setAutoGenerateProxyClasses(true);
  33.  
  34. $connectionOptions = array(
  35. 'driver' => 'pdo_mysql',
  36. 'user' => 'root',
  37. 'password' => '',
  38. 'dbname' => 'learningzf'
  39. );
  40.  
  41. // setup entity manager
  42. $em = \Doctrine\ORM\EntityManager::create($connectionOptions, $config);
  43. Zend_Registry::set("em", $em);
  44. return $em;
  45. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.