Esempio n. 1
0
 /**
  * Add the content of a Criteria to the current Criteria
  * In case of conflict, the current Criteria keeps its properties
  *
  * @param     Criteria $criteria The criteria to read properties from
  * @param     string $operator The logical operator used to combine conditions
  *            Defaults to Criteria::LOGICAL_AND, also accapts Criteria::LOGICAL_OR
  *            This parameter is deprecated, use _or() instead
  *
  * @return    Criteria The current criteria object
  */
 public function mergeWith(Criteria $criteria, $operator = null)
 {
     // merge limit
     $limit = $criteria->getLimit();
     if ($limit != 0 && $this->getLimit() == 0) {
         $this->limit = $limit;
     }
     // merge offset
     $offset = $criteria->getOffset();
     if ($offset != 0 && $this->getOffset() == 0) {
         $this->offset = $offset;
     }
     // merge select modifiers
     $selectModifiers = $criteria->getSelectModifiers();
     if ($selectModifiers && !$this->selectModifiers) {
         $this->selectModifiers = $selectModifiers;
     }
     // merge select columns
     $this->selectColumns = array_merge($this->getSelectColumns(), $criteria->getSelectColumns());
     // merge as columns
     $commonAsColumns = array_intersect_key($this->getAsColumns(), $criteria->getAsColumns());
     if (!empty($commonAsColumns)) {
         throw new PropelException('The given criteria contains an AsColumn with an alias already existing in the current object');
     }
     $this->asColumns = array_merge($this->getAsColumns(), $criteria->getAsColumns());
     // merge orderByColumns
     $orderByColumns = array_merge($this->getOrderByColumns(), $criteria->getOrderByColumns());
     $this->orderByColumns = array_unique($orderByColumns);
     // merge groupByColumns
     $groupByColumns = array_merge($this->getGroupByColumns(), $criteria->getGroupByColumns());
     $this->groupByColumns = array_unique($groupByColumns);
     // merge where conditions
     if ($operator == Criteria::LOGICAL_OR) {
         $this->_or();
     }
     $isFirstCondition = true;
     foreach ($criteria->getMap() as $key => $criterion) {
         if ($isFirstCondition && $this->defaultCombineOperator == Criteria::LOGICAL_OR) {
             $this->addOr($criterion, null, null, false);
             $this->defaultCombineOperator == Criteria::LOGICAL_AND;
         } elseif ($this->containsKey($key)) {
             $this->addAnd($criterion);
         } else {
             $this->add($criterion);
         }
         $isFirstCondition = false;
     }
     // merge having
     if ($having = $criteria->getHaving()) {
         if ($this->getHaving()) {
             $this->addHaving($this->getHaving()->addAnd($having));
         } else {
             $this->addHaving($having);
         }
     }
     // merge alias
     $commonAliases = array_intersect_key($this->getAliases(), $criteria->getAliases());
     if (!empty($commonAliases)) {
         throw new PropelException('The given criteria contains an alias already existing in the current object');
     }
     $this->aliases = array_merge($this->getAliases(), $criteria->getAliases());
     // merge join
     $this->joins = array_merge($this->getJoins(), $criteria->getJoins());
     return $this;
 }
Esempio n. 2
0
 public function testAddSelectModifier()
 {
     $c = new Criteria();
     $c->setDistinct();
     $c->addSelectModifier('SQL_CALC_FOUND_ROWS');
     $this->assertEquals(array(Criteria::DISTINCT, 'SQL_CALC_FOUND_ROWS'), $c->getSelectModifiers(), 'addSelectModifier() adds a select modifier to the Criteria');
     $c->addSelectModifier('SQL_CALC_FOUND_ROWS');
     $this->assertEquals(array(Criteria::DISTINCT, 'SQL_CALC_FOUND_ROWS'), $c->getSelectModifiers(), 'addSelectModifier() adds a select modifier only once');
     $params = array();
     $result = BasePeer::createSelectSql($c, $params);
     $this->assertEquals('SELECT DISTINCT SQL_CALC_FOUND_ROWS  FROM ', $result, 'addSelectModifier() adds a modifier to the final query');
 }
Esempio n. 3
0
 /**
  * Executes a COUNT query using either a simple SQL rewrite or, for more complex queries, a
  * sub-select of the SQL created by createSelectSql() and returns the statement.
  *
  * @param      Criteria $criteria A Criteria.
  * @param      ConnectionInterface $con A ConnectionInterface connection to use.
  * @return     Propel\Runtime\Connection\StatementInterface The resultset statement.
  * @throws     \Propel\Runtime\Exception\RuntimeException
  * @see        createSelectSql()
  */
 public static function doCount(Criteria $criteria, ConnectionInterface $con = null)
 {
     $dbMap = Propel::getServiceContainer()->getDatabaseMap($criteria->getDbName());
     $db = Propel::getServiceContainer()->getAdapter($criteria->getDbName());
     if ($con === null) {
         $con = Propel::getServiceContainer()->getReadConnection($criteria->getDbName());
     }
     $stmt = null;
     $needsComplexCount = $criteria->getGroupByColumns() || $criteria->getOffset() || $criteria->getLimit() || $criteria->getHaving() || in_array(Criteria::DISTINCT, $criteria->getSelectModifiers());
     try {
         $params = array();
         if ($needsComplexCount) {
             if (self::needsSelectAliases($criteria)) {
                 if ($criteria->getHaving()) {
                     throw new RuntimeException('Propel cannot create a COUNT query when using HAVING and  duplicate column names in the SELECT part');
                 }
                 $db->turnSelectColumnsToAliases($criteria);
             }
             $selectSql = self::createSelectSql($criteria, $params);
             $sql = 'SELECT COUNT(*) FROM (' . $selectSql . ') propelmatch4cnt';
         } else {
             // Replace SELECT columns with COUNT(*)
             $criteria->clearSelectColumns()->addSelectColumn('COUNT(*)');
             $sql = self::createSelectSql($criteria, $params);
         }
         $stmt = $con->prepare($sql);
         $db->bindValues($stmt, $params, $dbMap);
         $stmt->execute();
     } catch (Exception $e) {
         if ($stmt !== null) {
             $stmt = null;
         }
         Propel::log($e->getMessage(), Propel::LOG_ERR);
         throw new RuntimeException(sprintf('Unable to execute COUNT statement [%s]', $sql), 0, $e);
     }
     return $stmt;
 }
Esempio n. 4
0
 public function testMergeWithSelectModifiers()
 {
     $c1 = new Criteria();
     $c1->setDistinct();
     $c2 = new Criteria();
     $c1->mergeWith($c2);
     $this->assertEquals(array(Criteria::DISTINCT), $c1->getSelectModifiers(), 'mergeWith() does not remove an existing select modifier');
     $c1 = new Criteria();
     $c2 = new Criteria();
     $c2->setDistinct();
     $c1->mergeWith($c2);
     $this->assertEquals(array(Criteria::DISTINCT), $c1->getSelectModifiers(), 'mergeWith() merges the select modifiers');
     $c1 = new Criteria();
     $c1->setDistinct();
     $c2 = new Criteria();
     $c2->setDistinct();
     $c1->mergeWith($c2);
     $this->assertEquals(array(Criteria::DISTINCT), $c1->getSelectModifiers(), 'mergeWith() does not duplicate select modifiers');
     $c1 = new Criteria();
     $c1->setAll();
     $c2 = new Criteria();
     $c2->setDistinct();
     $c1->mergeWith($c2);
     $this->assertEquals(array(Criteria::ALL), $c1->getSelectModifiers(), 'mergeWith() does not merge the select modifiers in case of conflict');
 }