/** * collectMetadata for all Entities */ private function collectMeta() { $parts = $this->queryBuilder->getDQLParts(); $entity = reset($parts['from']); $this->setEntityName($entity->getFrom()); $this->setAlias($entity->getAlias()); $mdf = $this->queryBuilder->getEntityManager()->getMetadataFactory(); $this->meta[$this->alias] = $mdf->getMetadataFor($this->entityName); if (isset($parts['join'][$this->alias])) { /** @var Join $join */ foreach ($parts['join'][$this->alias] as $join) { $j = explode('.', $join->getJoin(), 2); if (count($j) != 2) { throw new \InvalidArgumentException('Join in wrong format: ' . $join->getJoin()); } if (!isset($this->meta[$j[0]])) { throw new \InvalidArgumentException('Unknown alias in join or wrong order: ' . $join->getJoin()); } if (!isset($this->meta[$j[0]]->associationMappings[$j[1]])) { throw new \InvalidArgumentException('Unknown Mapping in join: ' . $join->getJoin()); } $jEntity = $this->meta[$j[0]]->associationMappings[$j[1]]['targetEntity']; $this->meta[$join->getAlias()] = $mdf->getMetadataFor($jEntity); } } }
/** * Get optimized query builder for count calculation. * * @param QueryBuilder $originalQb * @return QueryBuilder */ public function getCountQueryBuilder(QueryBuilder $originalQb) { $this->setOriginalQueryBuilder($originalQb); $parts = $this->originalQb->getDQLParts(); $qb = clone $this->originalQb; $qb->setFirstResult(null)->setMaxResults(null)->resetDQLPart('orderBy')->resetDQLPart('groupBy')->resetDQLPart('select')->resetDQLPart('join')->resetDQLPart('where')->resetDQLPart('having'); $fieldsToSelect = array(); if ($parts['groupBy']) { $groupBy = (array) $parts['groupBy']; $groupByFields = $this->getSelectFieldFromGroupBy($groupBy); $usedGroupByAliases = []; foreach ($groupByFields as $key => $groupByField) { $alias = '_groupByPart' . $key; $usedGroupByAliases[] = $alias; $fieldsToSelect[] = $groupByField . ' as ' . $alias; } $qb->groupBy(implode(', ', $usedGroupByAliases)); } elseif (!$parts['where'] && $parts['having']) { // If there is no where and group by, but having is present - convert having to where. $parts['where'] = $parts['having']; $parts['having'] = null; $qb->resetDQLPart('having'); } if ($parts['having']) { $qb->having($this->qbTools->replaceAliasesWithFields($parts['having'])); } $hasJoins = false; if ($parts['join']) { $hasJoins = $this->addJoins($qb, $parts); } if (!$parts['groupBy']) { $qb->distinct($hasJoins); $fieldsToSelect[] = $this->getFieldFQN($this->idFieldName); } if ($parts['where']) { $qb->where($this->qbTools->replaceAliasesWithFields($parts['where'])); } $qb->select(array_unique($fieldsToSelect)); $this->qbTools->fixUnusedParameters($qb); return $qb; }
public function getResult($itemsPerPage, $page, Result $result) { $parts = $this->query->getDQLParts(); $from = $parts['from'][0]; $itemsOnPage = clone $this->query; $itemsOnPage->select('DISTINCT(' . $from->getAlias() . '.id)')->addSelect('(' . $this->getNumRowsSubQuery() . ') as num_rows')->from($from->getFrom(), 'master')->setMaxResults($itemsPerPage)->setFirstResult($itemsPerPage * ($page - 1)); $itemsOnPage = $itemsOnPage->getQuery()->getScalarResult(); $totalResults = 0; $ids = array(); if ($itemsOnPage) { $totalResults = $itemsOnPage[0]['num_rows']; foreach ($itemsOnPage as $itemOnPage) { $ids[] = $itemOnPage[1]; } } $totalPages = ceil($totalResults / $itemsPerPage); $this->query->resetDQLPart('where'); $this->query->where($from->getAlias() . ' IN (:ids)')->setParameters(array('ids' => $ids)); $results = $this->query->getQuery()->getResult(); return $this->populateResult($result, $totalResults, $totalPages, $page, $results); }
public function draftizeQueryBuilder(\Doctrine\ORM\QueryBuilder $qb) { $parts = $qb->getDQLParts(); $froms = $parts['from']; foreach ($froms as $fromExpr) { $entityClass = $fromExpr->getFrom(); $entityAlias = $fromExpr->getAlias(); if (method_exists('\\' . $entityClass, 'getDraft') && !$this->allowDraft) { $qb->andWhere($entityAlias . '.draft=:draftAllowed')->setParameter('draftAllowed', false); } } return $qb; }
/** * @param \Doctrine\ORM\QueryBuilder $qb Query builder * @param string $rootAlias Root alias * @param string $joinAlias Join alias * * @return \Doctrine\ORM\Query\Expr\Join */ public static function findJoinByAlias(QueryBuilder $qb, $rootAlias, $joinAlias) { $dqlParts = $qb->getDQLParts(); if (!isset($dqlParts['join'][$rootAlias])) { return null; } /** @var \Doctrine\ORM\Query\Expr\Join $join */ foreach ($dqlParts['join'][$rootAlias] as $join) { if ($joinAlias === $join->getAlias()) { return $join; } } return null; }
/** * @author Andreas Glaser */ protected function buildPathToAliasMap() { $rootAlias = ArrayHelper::getFirstValue($this->qb->getRootAliases()); $this->aliasMap[$rootAlias] = $rootAlias; if (array_key_exists($rootAlias, $this->qb->getDQLParts()['join'])) { /** @var Expr\Join $part */ foreach ($this->qb->getDQLParts()['join'][$rootAlias] as $part) { $alias = $part->getAlias(); $join = $part->getJoin(); $path = $alias; $pieces = explode('.', $join); if ($parentAlias = ArrayHelper::getKeyByValue($this->aliasMap, $pieces[0])) { $path = $parentAlias . '.' . $alias; } $this->aliasMap[$path] = $alias; } } }