/**
  * @param NQLQuery $nqlQuery
  * @param bool $skipSelection
  * @return QueryBuilder
  * @throws SyntaxErrorException
  */
 public function convert(NQLQuery $nqlQuery, bool $skipSelection) : QueryBuilder
 {
     $this->checkTables($nqlQuery->getFrom());
     /** @var QueryBuilder $query */
     $query = $this->em->createQueryBuilder();
     /** @var string $columnDefaultAlias */
     $columnDefaultAlias = count($nqlQuery->getFrom()->getTables()) === 1 ? $nqlQuery->getFrom()->getTables()[0]->getAlias() : '';
     if ($columnDefaultAlias === 'group') {
         $columnDefaultAlias = '_group';
     }
     if ($skipSelection) {
         $query->select($columnDefaultAlias);
     } else {
         if ($nqlQuery->getSelect()->getColumns()) {
             /** @var Column $column */
             foreach ($nqlQuery->getSelect()->getColumns() as $column) {
                 $query->addSelect(($column->getAlias() ?? $columnDefaultAlias) . '.' . $column->getName());
             }
         } else {
             $query->select($columnDefaultAlias);
         }
     }
     foreach ($nqlQuery->getFrom()->getTables() as $table) {
         $query->from($this->entities[strtolower($table->getName())], $table->getAlias());
     }
     if ($nqlQuery->getWhere()->getConditions()) {
         $paramWhere = $this->getParametrizedWhere($nqlQuery->getWhere()->getConditions(), $columnDefaultAlias);
         $query->where($paramWhere['clause'])->setParameters($paramWhere['params']);
     }
     $columns = $this->getAllColumns($skipSelection ? Select::getBlank() : $nqlQuery->getSelect(), $nqlQuery->getWhere(), $nqlQuery->getOrderBy());
     $alreadyJoined = [];
     foreach ($columns as $column) {
         if (count($column->getJoinWith())) {
             $joinWith = $column->getJoinWith();
             $iMax = count($joinWith);
             for ($i = 0; $i < $iMax; $i++) {
                 if (!array_key_exists($joinWith[$i], $alreadyJoined)) {
                     if ($i === 0) {
                         $column = ($column->getAlias() === null ? $columnDefaultAlias : $column->getAlias()) . '.' . $column->getJoinWith()[$i];
                     } else {
                         $column = $joinWith[$i - 1] . '.' . $joinWith[$i];
                     }
                     $query->innerJoin($column, $joinWith[$i]);
                     $alreadyJoined[$joinWith[$i]] = null;
                 }
             }
         }
     }
     /** @var OrderingColumn $column */
     foreach ($nqlQuery->getOrderBy()->getColumns() as $column) {
         $query->addOrderBy((count($column->getJoinWith()) ? $column->getJoinWith()[count($column->getJoinWith()) - 1] : ($column->getAlias() === null ? $columnDefaultAlias : $column->getAlias())) . '.' . $column->getName(), $column->getOrdering());
     }
     if (null !== $nqlQuery->getLimit()) {
         $query->setMaxResults($nqlQuery->getLimit());
     }
     if (null !== $nqlQuery->getOffset()) {
         $query->setFirstResult($nqlQuery->getOffset());
     }
     return $query;
 }
Beispiel #2
0
 /**
  * @param NQLQuery $nqlQuery
  * @param bool $skipSelection
  *
  * @return mixed|string
  * @throws \Trinity\Bundle\SearchBundle\Exception\SyntaxErrorException
  */
 public function convertToJson(NQLQuery $nqlQuery, bool $skipSelection)
 {
     $entities = $nqlQuery->getQueryBuilder($skipSelection)->getQuery()->getResult();
     if (!$skipSelection) {
         return SerializerBuilder::create()->setPropertyNamingStrategy(new SerializedNameAnnotationStrategy(new PassThroughNamingStrategy()))->build()->serialize($entities, 'json');
     }
     $result = [];
     /** @var Select $select */
     $select = $nqlQuery->getSelect();
     foreach ($entities as $entity) {
         $result[] = $this->select($select->getColumns(), $entity);
     }
     $context = new SerializationContext();
     $context->setSerializeNull(true);
     return SerializerBuilder::create()->setPropertyNamingStrategy(new SerializedNameAnnotationStrategy(new PassThroughNamingStrategy()))->build()->serialize($result, 'json', $context);
 }