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())); }
/** * Adaptador Doctrine para o paginador do Zend * * @param $doctrineQuery * @param $modelName * @param $entityPath */ public function __construct($doctrineQuery, $modelName, $entityPath) { $this->_queryBuilder = $doctrineQuery; $this->_entityPath = $entityPath; $this->_modelName = $modelName; $this->_query = clone $this->_queryBuilder; $this->_query->select('e'); $this->_query->from($this->_entityPath, 'e'); }
/** * Add a select statement. * * @param associationField.fkField * @param $field */ public function select($fields) { foreach ($fields as $key => $value) { if (is_int($key)) { $valor = $value; if (is_array($value)) { if (!empty($value['expression'])) { //é uma expressão $expression = ['expression' => $value['expression'], 'alias' => $value['alias']]; $valor = $value['field']; $this->associationQueryFields($valor, $expression); } } else { $this->associationQueryFields($valor); } } elseif (is_array($value)) { $alias = $this->tableAlias(); $this->queryBuilder->join($this->getFullFieldName($key, self::DEFAULT_TABLE_ALIAS), $alias); foreach ($value as $valueField) { $this->queryFields[] = $this->getFullFieldName($valueField, $alias); } } } $this->queryBuilder->select(implode(',', $this->queryFields)); return $this; }
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; }
/** * 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(); }
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; }
/** * 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'); }
/** * Is called by the constructor. Creates an initializes the query builder. * The Entity is set, default ordering by date descending is set */ private function initializeQueryBuilder() { $this->qb = $this->em->createQueryBuilder(); $this->qb->select('e')->from($this->unifiedSocialEntityClass, 'e')->orderBy('e.created', 'DESC'); }
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; }
/** * 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'); }
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 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; }
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; }