/**
  * Returns id of an email entity corresponding given criteria
  *
  * @param Criteria|array $criteria
  * @param array          $joins
  *
  * @return int|null
  */
 public function findEmailId($criteria, $joins)
 {
     $criteria = $this->normalizeCriteria($criteria);
     $qb = $this->getRepository()->createQueryBuilder('e')->select('partial e.{id}')->setMaxResults(2);
     $this->applyJoins($qb, $joins);
     // fix of doctrine error with Same Field, Multiple Values, Criteria and QueryBuilder
     // http://www.doctrine-project.org/jira/browse/DDC-2798
     // TODO revert changes when doctrine version >= 2.5 in scope of BAP-5577
     QueryBuilderHelper::addCriteria($qb, $criteria);
     // $qb->addCriteria($criteria);
     /** @var Email[] $entity */
     $entity = $qb->getQuery()->getResult();
     if (!$entity || count($entity) > 1) {
         return null;
     }
     $this->checkFoundEntity($entity[0]);
     return $entity[0]->getId();
 }
Beispiel #2
0
 /**
  * Returns query builder that could be used for fetching data based on given filtering criteria
  *
  * @param int   $limit
  * @param int   $page
  * @param array $criteria
  * @param null  $orderBy
  * @param array $joins
  *
  * @return QueryBuilder|SqlQueryBuilder|SearchQuery|null
  */
 public function getListQueryBuilder($limit = 10, $page = 1, $criteria = [], $orderBy = null, $joins = [])
 {
     $criteria = $this->prepareQueryCriteria($limit, $page, $criteria, $orderBy);
     $qb = $this->getRepository()->createQueryBuilder('e');
     $this->applyJoins($qb, $joins);
     // fix of doctrine error with Same Field, Multiple Values, Criteria and QueryBuilder
     // http://www.doctrine-project.org/jira/browse/DDC-2798
     // TODO revert changes when doctrine version >= 2.5 in scope of BAP-5577
     QueryBuilderHelper::addCriteria($qb, $criteria);
     // $qb->addCriteria($criteria);
     return $qb;
 }
 /**
  * Returns a query builder that could be used for fetching the list of entities
  * associated with $associationOwnerClass entities found by $filters and $joins
  *
  * @param string      $associationTargetClass The FQCN of the entity that is the owning side of the association
  * @param mixed|null  $filters                Criteria is used to filter entities which are association owners
  *                                            e.g. ['age' => 20, ...] or \Doctrine\Common\Collections\Criteria
  * @param array|null  $joins                  Additional associations required to filter owning side entities
  * @param array       $associationOwners      The list of fields responsible to store associations between
  *                                            the given target and association owners
  *                                            Array format: [owner_entity_class => field_name]
  * @param int         $limit                  The maximum number of items per page
  * @param int         $page                   The page number
  * @param string|null $orderBy                The ordering expression for the result
  *
  * @return SqlQueryBuilder
  */
 public function getMultiAssociationOwnersQueryBuilder($associationTargetClass, $filters, $joins, $associationOwners, $limit = null, $page = null, $orderBy = null)
 {
     $em = $this->doctrineHelper->getEntityManager($associationTargetClass);
     $criteria = $this->doctrineHelper->normalizeCriteria($filters);
     $selectStmt = null;
     $subQueries = [];
     $targetIdFieldName = $this->doctrineHelper->getSingleEntityIdentifierFieldName($associationTargetClass);
     foreach ($associationOwners as $ownerClass => $fieldName) {
         // dispatch oro_api.request.get_list.before event
         $event = new GetListBefore($this->cloneCriteria($criteria), $associationTargetClass);
         $this->eventDispatcher->dispatch(GetListBefore::NAME, $event);
         $subCriteria = $event->getCriteria();
         $nameExpr = $this->entityNameResolver->getNameDQL($ownerClass, 'e');
         $subQb = $em->getRepository($ownerClass)->createQueryBuilder('e')->select(sprintf('target.%s AS id, e.id AS entityId, \'%s\' AS entityClass, ' . ($nameExpr ?: '\'\'') . ' AS entityTitle', $targetIdFieldName, str_replace('\'', '\'\'', $ownerClass)))->innerJoin('e.' . $fieldName, 'target');
         $this->doctrineHelper->applyJoins($subQb, $joins);
         // fix of doctrine error with Same Field, Multiple Values, Criteria and QueryBuilder
         // http://www.doctrine-project.org/jira/browse/DDC-2798
         // TODO revert changes when doctrine version >= 2.5 in scope of BAP-5577
         QueryBuilderHelper::addCriteria($subQb, $subCriteria);
         // $subQb->addCriteria($criteria);
         $subQuery = $subQb->getQuery();
         $subQueries[] = QueryUtils::getExecutableSql($subQuery);
         if (empty($selectStmt)) {
             $mapping = QueryUtils::parseQuery($subQuery)->getResultSetMapping();
             $selectStmt = sprintf('entity.%s AS id, entity.%s AS entity, entity.%s AS title', QueryUtils::getColumnNameByAlias($mapping, 'entityId'), QueryUtils::getColumnNameByAlias($mapping, 'entityClass'), QueryUtils::getColumnNameByAlias($mapping, 'entityTitle'));
         }
     }
     $rsm = new ResultSetMapping();
     $rsm->addScalarResult('id', 'id', Type::INTEGER)->addScalarResult('entity', 'entity')->addScalarResult('title', 'title');
     $qb = new SqlQueryBuilder($em, $rsm);
     $qb->select($selectStmt)->from('(' . implode(' UNION ALL ', $subQueries) . ')', 'entity');
     if (null !== $limit) {
         $qb->setMaxResults($limit);
         if (null !== $page) {
             $qb->setFirstResult($this->doctrineHelper->getPageOffset($page, $limit));
         }
     }
     if ($orderBy) {
         $qb->orderBy($orderBy);
     }
     return $qb;
 }