예제 #1
0
파일: Query.php 프로젝트: Maksold/platform
 /**
  * Returns string representation of the query
  *
  * @return string
  */
 public function getStringQuery()
 {
     $selectString = $this->getQuery();
     $fromString = '';
     if ($this->getFrom()) {
         $fromString .= ' from ' . implode(', ', $this->getFrom());
     }
     $visitor = new QueryStringExpressionVisitor();
     $whereString = ' where ' . $this->criteria->getWhereExpression()->visit($visitor);
     $orderByString = '';
     if ($this->getOrderBy()) {
         $orderByString .= ' ' . $this->getOrderBy();
     }
     if ($this->getOrderDirection()) {
         $orderByString .= ' ' . $this->getOrderDirection();
     }
     if ($orderByString) {
         $orderByString = ' order by' . $orderByString;
     }
     $limitString = '';
     if ($this->getMaxResults() && $this->getMaxResults() != Query::INFINITY) {
         $limitString = ' limit ' . $this->getMaxResults();
     }
     $offsetString = '';
     if ($this->getFirstResult()) {
         $offsetString .= ' offset ' . $this->getFirstResult();
     }
     return $selectString . $fromString . $whereString . $orderByString . $limitString . $offsetString;
 }
 /**
  * {@inheritdoc}
  */
 public function walkComparison(Comparison $comparison)
 {
     list($type, $field) = Criteria::explodeFieldTypeName($comparison->getField());
     $value = $comparison->getValue()->getValue();
     if (is_array($value)) {
         $value = sprintf('(%s)', implode(', ', $value));
     }
     if ($type === Query::TYPE_TEXT) {
         $value = sprintf('"%s"', $value);
     }
     return sprintf('%s %s %s %s', $type, $field, Criteria::getSearchOperatorByComparisonOperator($comparison->getOperator()), $value);
 }
 /**
  * Returns the context for the given activity class and id
  *
  * @param string $class The FQCN of the activity entity
  * @param        $id
  *
  * @return array
  */
 public function getActivityContext($class, $id)
 {
     $criteria = Criteria::create();
     $criteria->andWhere(Criteria::expr()->eq('id', $id));
     $currentUser = $this->securityTokenStorage->getToken()->getUser();
     $userClass = ClassUtils::getClass($currentUser);
     $queryBuilder = $this->activityManager->getActivityTargetsQueryBuilder($class, $criteria, null, null, null, null, function (QueryBuilder $qb, $targetEntityClass) use($currentUser, $userClass) {
         if ($targetEntityClass === $userClass) {
             // Exclude current user from result
             $qb->andWhere($qb->expr()->neq(QueryUtils::getSelectExprByAlias($qb, 'entityId'), $currentUser->getId()));
         }
     });
     if (null === $queryBuilder) {
         return [];
     }
     $result = $queryBuilder->getQuery()->getResult();
     if (empty($result)) {
         return $result;
     }
     $entityProvider = $this->configManager->getProvider('entity');
     foreach ($result as &$item) {
         $config = $entityProvider->getConfig($item['entity']);
         $metadata = $this->configManager->getEntityMetadata($item['entity']);
         $safeClassName = $this->entityClassNameHelper->getUrlSafeClassName($item['entity']);
         $link = null;
         if ($metadata) {
             $link = $this->router->generate($metadata->getRoute(), ['id' => $item['id']]);
         } elseif ($link === null && ExtendHelper::isCustomEntity($item['entity'])) {
             // Generate view link for the custom entity
             $link = $this->router->generate('oro_entity_view', ['id' => $item['id'], 'entityName' => $safeClassName]);
         }
         $item['activityClassAlias'] = $this->entityAliasResolver->getPluralAlias($class);
         $item['entityId'] = $id;
         $item['targetId'] = $item['id'];
         $item['targetClassName'] = $safeClassName;
         $item['icon'] = $config->get('icon');
         $item['link'] = $link;
         unset($item['id'], $item['entity']);
     }
     return $result;
 }
예제 #4
0
 /**
  * @param string $fieldString
  *
  * @return array
  */
 protected function explodeCombinedFieldString($fieldString)
 {
     list($type, $field) = Criteria::explodeFieldTypeName($fieldString);
     if (strpos($field, '|') !== false) {
         $field = explode('|', $field);
     }
     return [$type, $field];
 }
예제 #5
0
 /**
  * Set order by for search query
  *
  * @param \Oro\Bundle\SearchBundle\Query\Query $query
  * @param \Doctrine\ORM\QueryBuilder           $qb
  */
 protected function addOrderBy(Query $query, QueryBuilder $qb)
 {
     $orderBy = $query->getCriteria()->getOrderings();
     if ($orderBy) {
         $direction = reset($orderBy);
         list($fieldType, $fieldName) = Criteria::explodeFieldTypeName(key($orderBy));
         $orderRelation = $fieldType . 'Fields';
         $qb->leftJoin('search.' . $orderRelation, 'orderTable', 'WITH', 'orderTable.field = :orderField')->orderBy('orderTable.value', $direction)->setParameter('orderField', $fieldName);
     }
 }
예제 #6
0
파일: Parser.php 프로젝트: Maksold/platform
 /**
  * @return \Doctrine\Common\Collections\ExpressionBuilder|mixed
  */
 protected function parseCompositeCondition()
 {
     $expressions = [];
     $this->stream->next();
     while (!$this->stream->current->test(Token::PUNCTUATION_TYPE, ')')) {
         $type = null;
         if ($this->stream->current->test(Token::OPERATOR_TYPE)) {
             $type = $this->stream->current->value;
         }
         list($typeX, $expression) = $this->parseSimpleCondition($type);
         $expressions[] = ['type' => $typeX, 'expr' => $expression];
     }
     $expr = Criteria::expr();
     if ($expressions) {
         $expressions = array_reverse($expressions);
         $typeX = $expressions[0]['type'];
         $expressions = array_map(function ($item) use($typeX, $expressions) {
             if ($item['type'] !== $typeX && $item != end($expressions)) {
                 throw new ExpressionSyntaxError(sprintf('Syntax error. Composite operators of different types are not allowed on single level.'), $this->stream->current->cursor);
             }
             return $item['expr'];
         }, $expressions);
         $expr = call_user_func_array([$expr, str_replace('Where', 'X', $typeX)], $expressions);
     } else {
         throw new ExpressionSyntaxError(sprintf('Syntax error in composite expression.'), $this->stream->current->cursor);
     }
     $this->stream->next();
     return $expr;
 }