public function testNext()
 {
     $expectedResults = array(array(1), array(5), array(7));
     $query = new MockQuery();
     $query->setNextResult($expectedResults);
     $createQuery = function ($dql) use($query) {
         $query->setDQL($dql);
         return $query;
     };
     $this->em->expects($this->any())->method('createQuery')->will($this->returnCallback($createQuery));
     $qb = new \Doctrine\ORM\QueryBuilder($this->em);
     $qb->select('a.id, a.name')->from('A', 'a')->where('a.name = :name')->setParameter('name', 'abc');
     // initialize Iterator
     $this->object->setQueryBuilder($qb);
     $this->object->setIterateBy('a.id');
     $this->object->setPageSize(3);
     $this->object->setPullClosure(function ($arr) {
         return $arr[0];
     });
     // initial results
     $this->assertEquals($expectedResults, $this->object->next());
     $this->assertEquals('SELECT a.id, a.name FROM A a WHERE a.name = :name ORDER BY a.id ASC', $query->getDQL());
     $this->assertEquals(3, $query->getLastMaxResults());
     $this->assertEquals(1, \count($query->getLastParameters()));
     // second results
     $query->setNextResult(array());
     $this->assertEquals(array(), $this->object->next());
     $this->assertEquals('SELECT a.id, a.name FROM A a WHERE a.name = :name AND a.id > 7 ORDER BY a.id ASC', $query->getDQL());
     $this->assertEquals(3, $query->getLastMaxResults());
     $this->assertEquals(1, \count($query->getLastParameters()));
 }
Ejemplo n.º 2
0
 public function getFriendsFriendShips($userId)
 {
     $queryBuilder = new \Doctrine\ORM\QueryBuilder($this->entityManager);
     $queryBuilder->select("ffs")->from(self::MODEL, "ffs")->join("ffs.user_source", "f")->join("f.friendships_as_target", "mfs")->join("mfs.user_source", "me")->where("me.id = :user_id")->andWhere("ffs.accepted = 1")->andWhere("mfs.accepted = 1")->groupBy("ffs.user_target")->setParameter("user_id", $userId);
     $result = $this->getResults($queryBuilder->getQuery());
     return $result;
 }
 protected function initQueryFilter()
 {
     $em = $this->_getTestEntityManager();
     $qb = new \Doctrine\ORM\QueryBuilder($em);
     $query = $qb->select('q')->from('Admingenerator\\GeneratorBundle\\Tests\\QueryFilter\\Entity\\Movie', 'q');
     $queryFilter = new DoctrineQueryFilter($query);
     return $queryFilter;
 }
Ejemplo n.º 4
0
 /**
  * Sets sorting.
  * @param array $sorting
  */
 public function sort(array $sorting)
 {
     foreach ($sorting as $key => $value) {
         $column = isset($this->sortMapping[$key]) ? $this->sortMapping[$key] : $this->qb->getRootAlias() . '.' . $key;
         $this->qb->addOrderBy($column, $value);
     }
 }
Ejemplo n.º 5
0
 /**
  * Returns items matching the given criteria
  *
  * @param  int $offset Page offset
  * @param  int $itemCountPerPage Number of items per page
  * @return array
  */
 public function getItems($offset, $itemCountPerPage)
 {
     if ($offset > 0) {
         $this->query->setFirstResult($offset);
     }
     $this->query->setMaxResults($itemCountPerPage);
     return $this->query->select('e')->getQuery()->getResult();
 }
Ejemplo n.º 6
0
 /**
  * Returns the whole aggregated feed. You can limit the list giving any number.
  *
  * @return List<UnifiedSocialEntityInterface>
  */
 public function getFeed($limit = false)
 {
     if ($limit !== false && is_int($limit)) {
         $this->qb->setMaxResults($limit);
     }
     $this->qb->andWhere('e.approved = 1');
     return $this->qb->getQuery()->getResult();
 }
Ejemplo n.º 7
0
 /**
  * Is called by the constructor. Creates an initializes the query builder.
  * The Entity is set, default ordering by date descending is set
  *
  * @param string $requestId the request id for which the builder is initialized
  */
 protected function initializeQueryBuilder($requestId)
 {
     $found = false;
     foreach ($this->apiRequests as $apiRequest) {
         if ($apiRequest->getId() == $requestId) {
             $found = true;
             break;
         }
     }
     if (!$found) {
         throw new \InvalidArgumentException('The api request with ID ' . $requestId . ' could not be found!');
     }
     $this->qb = $this->em->createQueryBuilder();
     $this->qb->select('e')->from($apiRequest->getMappedEntity(), 'e')->orderBy('e.' . $apiRequest->getOrderField(), 'DESC');
 }
Ejemplo n.º 8
0
 /**
  * @return string
  */
 protected function tableAlias()
 {
     return self::DEFAULT_TABLE_ALIAS . count($this->queryBuilder->getAllAliases());
 }
 /**
  * Ordem
  *
  * @param $sCampo
  * @param $sOrdem
  */
 public function orderBy($sCampo, $sOrdem)
 {
     $this->_query->addOrderBy($sCampo, $sOrdem);
 }
Ejemplo n.º 10
0
 public function getListLikedByUser($userId)
 {
     $queryBuilder = new \Doctrine\ORM\QueryBuilder($this->entityManager);
     $queryBuilder->select("b")->from(self::MODEL, "b")->join("b.userbooks", "ub")->join("ub.user", "u")->where("u.id = :user_id")->andWhere("(ub.rating >= 4 OR ub.is_wished = 1)")->andWhere("ub.is_deleted != 1")->orderBy("ub.last_modification_date", "DESC")->setParameter("user_id", $userId);
     // We don't cache this result as the function is only called by the book svc which does the caching
     $result = $this->getResults($queryBuilder->getQuery());
     return $result;
 }
Ejemplo n.º 11
0
 public function getListWishedBooks($userId, $limit, $useCache)
 {
     $cacheId = $this->getCacheId(__FUNCTION__, array($userId));
     $queryBuilder = new \Doctrine\ORM\QueryBuilder($this->entityManager);
     $queryBuilder->select("ub, u, b, r, p, c")->from(self::MODEL, "ub")->join("ub.user", "u")->join("ub.book", "b")->leftJoin("ub.reading_state", "r")->leftJoin("b.publisher", "p")->leftJoin("b.contributors", "c")->orderBy("ub.id", "DESC")->where("u.id = :id")->andWhere("ub.is_deleted = 0")->andWhere("ub.is_wished = 1")->setParameter("id", $userId);
     if ($limit != -1) {
         $queryBuilder->setMaxResults($limit);
     }
     $result = $this->getResults($queryBuilder->getQuery(), $cacheId, $useCache);
     return $result;
 }
Ejemplo n.º 12
0
 /**
  * Creates query for given filters in stored in session
  *
  * @param array                     $sessionData  Filters
  * @param Doctrine\ORM\Query\Expr   $query        Query operator
  * @param Doctrine\ORM\QueryBuilder $queryBuilder Query builder
  *
  * @return Doctrine\ORM\Query\Expr
  */
 private function buildSessionFilters($sessionData, $query, $queryBuilder)
 {
     foreach ($sessionData as $key => $value) {
         if ($key) {
             if ($key == 'filterApproved') {
                 $query->add($queryBuilder->expr()->eq('c.status', 0));
             } else {
                 $query->add($queryBuilder->expr()->eq('c.status', $value));
             }
         }
     }
     return $query;
 }
Ejemplo n.º 13
0
 /**
  * Is called by the constructor. Creates an initializes the query builder.
  * The Entity is set, default ordering by date descending is set
  */
 protected function initializeQueryBuilder()
 {
     $this->qb = $this->em->createQueryBuilder();
     $this->qb->select('e')->from($this->socialEntityClass, 'e')->orderBy('e.snippetPublishedAt', 'DESC');
 }
Ejemplo n.º 14
0
 /**
  * 
  * @param  Doctrine\ORM\QueryBuilder  $builder
  * @return Doctrine\ORM\QueryBuilder
  */
 protected function applyLivingFilter($builder)
 {
     return $builder->where('e.setupDate > :twoMonthsAgo')->andWhere('e.trashed = false')->setParameter('twoMonthsAgo', $this->twoMonthsAgo()->format('Y-m-d'));
 }
Ejemplo n.º 15
0
 /**
  * @param int id
  * @return EntityFinder
  */
 public function whereId($id)
 {
     $this->qb->andWhere("{$this->alias}.id = :id");
     $this->qb->setParameter("id", $id);
     return $this;
 }
Ejemplo n.º 16
0
 /**
  * Add name first letter where condition to query builder
  *
  * @param  Doctrine\ORM\QueryBuilder $qb
  * @param  array                     $letters
  * @return void
  */
 private function addNameRangeWhere($qb, array $letters)
 {
     $orx = $qb->expr()->orx();
     foreach ($letters as $letter) {
         $orx->add($qb->expr()->like('u.username', $qb->expr()->literal(substr($letter, 0, 1) . '%')));
     }
     $qb->andWhere($orx);
 }
Ejemplo n.º 17
0
 public function getListByKeyword($keyword)
 {
     $queryBuilder = new \Doctrine\ORM\QueryBuilder($this->entityManager);
     $queryBuilder->select("u")->from(self::MODEL, "u")->Where("u.email LIKE :keyword")->orWhere("u.user_name LIKE :keyword")->orWhere("u.last_name LIKE :keyword")->orWhere("u.first_name LIKE :keyword")->setParameter("keyword", "%" . $keyword . "%");
     $result = $this->getResults($queryBuilder->getQuery());
     return $result;
 }
Ejemplo n.º 18
0
 public function import($row)
 {
     $descriptions = [];
     switch ($this->importBehaviour) {
         case self::IMPORTBEHAVIOUR_ALWAYSSETTO:
             $targetEntity = $this->iriConverter->getItemFromIri($this->setToEntity);
             $this->log(sprintf("Would set %s to %s#%s", $this->associationName, $this->baseEntity, $targetEntity->getId()));
             return $targetEntity;
             break;
         case self::IMPORTBEHAVIOUR_MATCHDATA:
             $configuration = [];
             foreach ($this->matchers as $matcher) {
                 $foo = new \stdClass();
                 $foo->property = $matcher->matchField;
                 $foo->operator = "=";
                 $foo->value = $row[$matcher->importField];
                 $descriptions[] = sprintf("%s = %s", $matcher->matchField, $row[$matcher->importField]);
                 $configuration[] = $foo;
             }
             $configuration = $this->advancedSearchFilter->extractConfiguration($configuration, []);
             $filters = $configuration['filters'];
             $sorters = $configuration['sorters'];
             $qb = new \Doctrine\ORM\QueryBuilder($this->em);
             $qb->select("o")->from($this->baseEntity, "o");
             $this->advancedSearchFilter->filter($qb, $filters, $sorters);
             try {
                 $result = $qb->getQuery()->getSingleResult();
                 if ($this->updateBehaviour === self::UPDATEBEHAVIOUR_UPDATEDATA) {
                     // @todo Update the entity with the specified values
                 }
                 $this->log(sprintf("Would set %s to %s#%s", $this->associationName, $this->baseEntity, $result->getId()));
                 return $result;
             } catch (\Exception $e) {
             }
             switch ($this->notFoundBehaviour) {
                 case self::NOTFOUNDBEHAVIOUR_STOPIMPORT:
                     $this->log(sprintf("Would stop import as the match %s for association %s was not found", implode(",", $descriptions), $this->getAssociationName()));
                     break;
                 case self::NOTFOUNDBEHAVIOUR_SETTOENTITY:
                     $targetEntity = $this->iriConverter->getItemFromIri($this->notFoundSetToEntity);
                     $this->log(sprintf("Would set the association %s to %s, since the match %s for association %s was not found", $this->getAssociationName(), $this->notFoundSetToEntity, implode(",", $descriptions)));
                     return $targetEntity;
                     break;
                 case self::NOTFOUNDBEHAVIOUR_CREATEENTITY:
                     $this->log(sprintf("Would create a new entity of type %s", $this->baseEntity));
                     return parent::import($row);
                     break;
             }
             break;
     }
     return null;
 }
 public function where($predicates)
 {
     $this->_query->where($predicates);
     $this->_filter['where'][] = $predicates;
 }