public function applyGreatestNPerGroupDate(QueryBuilder $qb) { $qb->distinct(); $qb2 = $this->createQueryBuilder('ss')->select('MAX(ss.timestamp)')->where('DATE(ss.timestamp) = DATE(s.timestamp)'); $qb->innerJoin($this->getEntityName(), 'sub', 'WITH', $qb->expr()->eq('s.timestamp', '(' . $qb2->getDQL() . ')')); }
public function makeDistinct() { $this->queryBuilder->distinct(); }
/** * Applies the given joins for the query builder * * @param QueryBuilder $qb * @param array|null $joins */ public static function applyJoins(QueryBuilder $qb, $joins) { if (empty($joins)) { return; } $qb->distinct(true); $rootAlias = self::getSingleRootAlias($qb); foreach ($joins as $key => $val) { if (empty($val)) { $qb->leftJoin($rootAlias . '.' . $key, $key); } elseif (is_array($val)) { if (isset($val['join'])) { $join = $val['join']; if (false === strpos($join, '.')) { $join = $rootAlias . '.' . $join; } } else { $join = $rootAlias . '.' . $key; } $condition = null; $conditionType = null; if (isset($val['condition'])) { $condition = $val['condition']; $conditionType = Expr\Join::WITH; } if (isset($val['conditionType'])) { $conditionType = $val['conditionType']; } $qb->leftJoin($join, $key, $conditionType, $condition); } else { $qb->leftJoin($rootAlias . '.' . $val, $val); } } }
/** * {@inheritdoc} */ public function parse($value, QueryBuilder $qb) { if (!is_array($value)) { $value = Yaml::parse($value); } $processor = new Processor(); $value = $processor->processConfiguration(new QueryConfiguration(), $value); if (!isset($value['from'])) { throw new \RuntimeException('Missing mandatory "from" section'); } foreach ((array) $value['from'] as $from) { $qb->from($from['table'], $from['alias']); } if (isset($value['select'])) { foreach ($value['select'] as $select) { $qb->add('select', new Expr\Select($select), true); } } if (isset($value['distinct'])) { $qb->distinct((bool) $value['distinct']); } if (isset($value['groupBy'])) { $qb->groupBy($value['groupBy']); } if (isset($value['having'])) { $qb->having($value['having']); } $this->addJoin($qb, $value); $this->addWhere($qb, $value); $this->addOrder($qb, $value); return $qb; }
/** * {@inheritdoc} */ public function execute() { // emit listbuilder.create event $event = new ListBuilderCreateEvent($this); $this->eventDispatcher->dispatch(ListBuilderEvents::LISTBUILDER_CREATE, $event); $this->expressionFields = $this->getUniqueExpressionFieldDescriptors($this->expressions); // first create simplified id query // select ids with all necessary filter data $ids = $this->findIdsByGivenCriteria(); // if no results are found - return if (count($ids) < 1) { return []; } // now select all data $this->queryBuilder = $this->em->createQueryBuilder()->from($this->entityName, $this->entityName); $this->assignJoins($this->queryBuilder); // Add all select fields foreach ($this->selectFields as $field) { $this->queryBuilder->addSelect($field->getSelect() . ' AS ' . $field->getName()); } // group by $this->assignGroupBy($this->queryBuilder); // assign sort-fields $this->assignSortFields($this->queryBuilder); // use ids previously selected ids for query $select = $this->entityName . '.id'; if (null !== $this->idField) { $select = $this->idField->getSelect(); } $this->queryBuilder->where($select . ' IN (:ids)')->setParameter('ids', $ids); $this->queryBuilder->distinct($this->distinct); return $this->queryBuilder->getQuery()->getArrayResult(); }
/** * Recreate query builder and set state again. * * @return void */ public function __wakeup() { if ($this->constraint !== null) { $this->queryBuilder->where($this->constraint); } if (is_array($this->orderings)) { $aliases = $this->queryBuilder->getRootAliases(); foreach ($this->orderings as $propertyName => $order) { $this->queryBuilder->addOrderBy($aliases[0] . '.' . $propertyName, $order); } } if (is_array($this->joins)) { foreach ($this->joins as $joinAlias => $join) { $this->queryBuilder->leftJoin($join, $joinAlias); } } $this->queryBuilder->setFirstResult($this->offset); $this->queryBuilder->setMaxResults($this->limit); $this->queryBuilder->distinct($this->distinct); $this->queryBuilder->setParameters($this->parameters); unset($this->parameters); }