Ejemplo n.º 1
0
 /**
  * Initializes resource
  *
  * @return Doctrine\ORM\EntityManager
  */
 public function init()
 {
     $options = $this->getOptions();
     $config = new Configuration();
     if (APPLICATION_ENV == 'production' && function_exists('apc_fetch')) {
         // @codeCoverageIgnoreStart
         #extension_loaded() memcache, xcache, redis
         $cache = new ApcCache();
     } else {
         // @codeCoverageIgnoreEnd
         $cache = new ArrayCache();
     }
     #$driverImpl = $config->newDefaultAnnotationDriver($options['modelDirectory']);
     // @todo Temporary(?) fix for using new AnnotationReader
     $reader = new AnnotationReader();
     #$reader->setEnableParsePhpImports(true);
     $reader = new IndexedReader($reader);
     $reader = new CachedReader($reader, $cache);
     $driverImpl = new AnnotationDriver($reader, $options['modelDirectory']);
     class_exists('Doctrine\\ORM\\Mapping\\Driver\\DoctrineAnnotations');
     // Beware cache slams: http://doctrine-orm.readthedocs.org/en/2.0.x/reference/caching.html#cache-slams
     $config->setMetadataCacheImpl($cache);
     $config->setQueryCacheImpl($cache);
     $config->setResultCacheImpl($cache);
     $config->setProxyDir($options['proxyDirectory']);
     $config->setProxyNamespace($options['proxyNamespace']);
     $config->setAutoGenerateProxyClasses($options['autoGenerateProxyClasses']);
     $config->setMetadataDriverImpl($driverImpl);
     // @codeCoverageIgnoreStart
     if (null !== $options['logPath']) {
         $sqlLogger = new SqlLogger($options['logPath']);
         $config->setSQLLogger($sqlLogger);
         Zend_Registry::set('sqlLogger', $sqlLogger);
     }
     // @codeCoverageIgnoreEnd
     $entityManager = EntityManager::create($options['connection'], $config);
     Service::setEntityManager($entityManager);
     // Add BLOB data type mapping
     if (!Type::hasType('gzblob')) {
         Type::addType('gzblob', 'Rexmac\\Zyndax\\Doctrine\\DBAL\\Type\\GzBlob');
         $entityManager->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('BLOB', 'gzblob');
     }
     // Add IP data type mapping
     if (!Type::hasType('ip')) {
         Type::addType('ip', 'Rexmac\\Zyndax\\Doctrine\\DBAL\\Type\\Ip');
         $entityManager->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('IP', 'ip');
     }
     return $entityManager;
 }
Ejemplo n.º 2
0
 /**
  * Constructor
  *
  * @param string $entityClass Name of Doctrine entity to be paginated
  * @param string $sortField Name of Doctrine entity's property to sort results by
  * @param string [Optional] $sortOrder Sort order (i.e. 'ASC' or 'DESC'). Defaults to 'ASC'.
  * @param string [Optional] $where Where clause
  * @return void
  */
 public function __construct($entityClass, $sortField = null, $sortOrder = 'ASC', $where = null)
 {
     $this->entityClass = $entityClass;
     $this->sortField = $sortField;
     $this->sortOrder = $sortOrder;
     $this->where = $where;
     $queryBuilder = DoctrineService::getEntityManager()->createQueryBuilder();
     $queryBuilder->select('e')->from($this->entityClass, 'e');
     if (null !== $this->sortField) {
         $part = explode('.', $this->sortField);
         if (isset($part[1])) {
             $foreignColName = $part[1];
             $foreignEntityName = lcfirst(array_pop(explode('\\', $part[0])));
             $queryBuilder->leftJoin('e.' . $foreignEntityName, 'j')->orderBy('j.' . $foreignColName, $this->sortOrder);
         } else {
             $queryBuilder->orderBy('e.' . $this->sortField, $this->sortOrder);
         }
     }
     if (null !== $this->where && '' !== $this->where) {
         $queryBuilder->where($this->where);
     }
     $this->query = $queryBuilder;
     $this->countQuery = clone $this->query;
 }