Ver Mensaje Individual
  #3 (permalink)  
Antiguo 01/04/2013, 11:24
wilmer30
 
Fecha de Ingreso: enero-2010
Mensajes: 491
Antigüedad: 14 años, 10 meses
Puntos: 12
Respuesta: Recurso para integración doctrine en zf 1.12

Este sería mi resource.
Código PHP:
Ver original
  1. <?php
  2.  
  3. require_once 'Zend/Application/Resource/ResourceAbstract.php';
  4. require_once '/../../vendor/doctrine/common/lib/Doctrine/Common/ClassLoader.php';
  5.  
  6. use Doctrine\Common\ClassLoader,
  7.     Doctrine\ORM\Configuration,
  8.     Doctrine\ORM\EntityManager,
  9.     Doctrine\DBAL\Types\Type,
  10.     Doctrine\Common\Cache\ArrayCache,
  11.     Doctrine\DBAL\Logging\EchoSqlLogger;
  12.  
  13. class Doctrine_Zend_Doctrine extends Zend_Application_Resource_ResourceAbstract
  14. {
  15. public function init()
  16. {
  17.     $options = $this -> getOptions();
  18.  
  19.     // Doctrine (use include_path)
  20.     $classLoader = new \Doctrine\Common\ClassLoader('Doctrine');
  21.     $classLoader -> register();
  22.      
  23.     // Entities
  24.     $classLoader = new \Doctrine\Common\ClassLoader('Application\Models',dirname(APPLICATION_PATH) . DIRECTORY_SEPARATOR . 'library');
  25.     $classLoader -> register();
  26.     //Proxies
  27.     $classLoader = new \Doctrine\Common\ClassLoader ('Application\Proxies', dirname ( APPLICATION_PATH ) . DIRECTORY_SEPARATOR . 'library' );
  28.     $classLoader -> register();
  29.     //Repositories
  30.     $classLoader = new \Doctrine\Common\ClassLoader('Application\Repositories',dirname(APPLICATION_PATH) . DIRECTORY_SEPARATOR . 'library');
  31.     $classLoader -> register();
  32.        
  33.     // Now configure doctrine
  34.         if ('development' == APPLICATION_ENV) {
  35.          $cacheClass = isset($options['cacheClass']) ? $options['cacheClass'] : APPLICATION_PATH.'\library\vendor\doctrine\cache\lib\Doctrine\Common\Cache\ArrayCache';
  36.          //vendor\doctrine\cache\lib\Doctrine\Common\Cache\MemcacheCache
  37.         } else {
  38.          $cacheClass = isset($options['cacheClass']) ? $options['cacheClass'] : 'Doctrine\Common\Cache\ApcCache';
  39.         }
  40.         $cache = new $cacheClass();
  41.        
  42.         $config = new Configuration();
  43.         $config -> setMetadataCacheImpl($cache);
  44.         $config -> setMetadataDriverImpl(Doctrine\ORM\Mapping\Driver\AnnotationDriver::create(array($options['entitiesPath'])));
  45.         $config -> setQueryCacheImpl($cache);
  46.         $config -> setProxyDir($options['proxiesPath']);
  47.         $config -> setProxyNamespace('Application\Proxies');
  48.         $config -> setAutoGenerateProxyClasses(('development' == APPLICATION_PATH));
  49.         $em = EntityManager::create(
  50.             $this -> _buildConnectionOptions($options['connection']),
  51.             $config
  52.         );
  53.        
  54.         Zend_Registry::set('em', $em);
  55.        
  56.         // end
  57.         return $em;
  58. }
  59.  
  60. protected function _buildConnectionOptions(array $options)
  61. {
  62. $connectionSpec = array(
  63.             'pdo_sqlite'    => array('user', 'password', 'path', 'memory'),
  64.             'pdo_mysql'     => array('user', 'password', 'host', 'port', 'dbname', 'unix_socket'),
  65.             'pdo_pgsql'     => array('user', 'password', 'host', 'port', 'dbname'),
  66.             'pdo_oci'       => array('user', 'password', 'host', 'port', 'dbname', 'charset')
  67. );
  68.  
  69. $connection = array(
  70.             'driver' => $options['driver']
  71. );
  72.  
  73. foreach ($connectionSpec[$options['driver']] as $driverOption) {
  74.     if (isset($options[$driverOption]) && !is_null($driverOption)) {
  75.         $connection[$driverOption] = $options[$driverOption];
  76.     }
  77. }
  78.  
  79. if (isset($options['driverOptions']) && !is_null($options['driverOptions'])) {
  80.     $connection['driverOptions'] = $options['driverOptions'];
  81. }
  82.  
  83. return $connection;
  84. }
  85. }