Beispiel #1
0
 /**
  * Add criteria to query.
  * Add where expressions with AND operator.
  * Add orderings.
  * Override firstResult and maxResults if they set.
  *
  * @param Criteria $criteria
  * @return QueryBuilder
  */
 public function addCriteria(Criteria $criteria)
 {
     $visitor = new QueryExpressionVisitor();
     if ($whereExpression = $criteria->getWhereExpression()) {
         $this->andWhere($visitor->dispatch($whereExpression));
         foreach ($visitor->getParameters() as $parameter) {
             $this->parameters->add($parameter);
         }
     }
     if ($criteria->getOrderings()) {
         foreach ($criteria->getOrderings() as $sort => $order) {
             $this->addOrderBy($sort, $order);
         }
     }
     // Overwrite limits only if they was set in criteria
     if (($firstResult = $criteria->getFirstResult()) !== null) {
         $this->setFirstResult($firstResult);
     }
     if (($maxResults = $criteria->getMaxResults()) !== null) {
         $this->setMaxResults($maxResults);
     }
     return $this;
 }
Beispiel #2
0
 /**
  * Adds criteria to the query.
  *
  * Adds where expressions with AND operator.
  * Adds orderings.
  * Overrides firstResult and maxResults if they're set.
  *
  * @param Criteria $criteria
  * @return QueryBuilder
  * @throws Query\QueryException
  */
 public function addCriteria(Criteria $criteria)
 {
     $allAliases = $this->getAllAliases();
     if (!isset($allAliases[0])) {
         throw new Query\QueryException('No aliases are set before invoking addCriteria().');
     }
     $visitor = new QueryExpressionVisitor($this->getAllAliases());
     if ($whereExpression = $criteria->getWhereExpression()) {
         $this->andWhere($visitor->dispatch($whereExpression));
         foreach ($visitor->getParameters() as $parameter) {
             $this->parameters->add($parameter);
         }
     }
     if ($criteria->getOrderings()) {
         foreach ($criteria->getOrderings() as $sort => $order) {
             $hasValidAlias = false;
             foreach ($allAliases as $alias) {
                 if (strpos($sort . '.', $alias . '.') === 0) {
                     $hasValidAlias = true;
                     break;
                 }
             }
             if (!$hasValidAlias) {
                 $sort = $allAliases[0] . '.' . $sort;
             }
             $this->addOrderBy($sort, $order);
         }
     }
     // Overwrite limits only if they was set in criteria
     if (($firstResult = $criteria->getFirstResult()) !== null) {
         $this->setFirstResult($firstResult);
     }
     if (($maxResults = $criteria->getMaxResults()) !== null) {
         $this->setMaxResults($maxResults);
     }
     return $this;
 }
 public function testClearParameters()
 {
     $this->visitor->getParameters()->add(new Parameter('field', 'value'));
     $this->visitor->clearParameters();
     $this->assertCount(0, $this->visitor->getParameters());
 }