Ejemplo n.º 1
0
 /**
  * Ensures uniqueness of select column names by turning them all into aliases
  * This is necessary for queries on more than one table when the tables share a column name
  * Moved from BasePeer to PdoAdapter and turned from static to non static
  *
  * @see http://propel.phpdb.org/trac/ticket/795
  *
  * @param     Propel\Runtime\Query\Criteria  $criteria
  * @return    Propel\Runtime\Query\Criteria  The input, with Select columns replaced by aliases
  */
 public function turnSelectColumnsToAliases(Criteria $criteria)
 {
     $selectColumns = $criteria->getSelectColumns();
     // clearSelectColumns also clears the aliases, so get them too
     $asColumns = $criteria->getAsColumns();
     $criteria->clearSelectColumns();
     $columnAliases = $asColumns;
     // add the select columns back
     foreach ($selectColumns as $clause) {
         // Generate a unique alias
         $baseAlias = preg_replace('/\\W/', '_', $clause);
         $alias = $baseAlias;
         // If it already exists, add a unique suffix
         $i = 0;
         while (isset($columnAliases[$alias])) {
             $i++;
             $alias = $baseAlias . '_' . $i;
         }
         // Add it as an alias
         $criteria->addAsColumn($alias, $clause);
         $columnAliases[$alias] = $clause;
     }
     // Add the aliases back, don't modify them
     foreach ($asColumns as $name => $clause) {
         $criteria->addAsColumn($name, $clause);
     }
     return $criteria;
 }
Ejemplo n.º 2
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;
 }
Ejemplo n.º 3
0
 public function testMergeWithAsColumns()
 {
     $c1 = new Criteria();
     $c1->addAsColumn('foo', BookPeer::TITLE);
     $c1->addAsColumn('bar', BookPeer::ID);
     $c2 = new Criteria();
     $c1->mergeWith($c2);
     $this->assertEquals(array('foo' => BookPeer::TITLE, 'bar' => BookPeer::ID), $c1->getAsColumns(), 'mergeWith() does not remove an existing as columns');
     $c1 = new Criteria();
     $c2 = new Criteria();
     $c2->addAsColumn('foo', BookPeer::TITLE);
     $c2->addAsColumn('bar', BookPeer::ID);
     $c1->mergeWith($c2);
     $this->assertEquals(array('foo' => BookPeer::TITLE, 'bar' => BookPeer::ID), $c1->getAsColumns(), 'mergeWith() merges the select columns to an empty as');
     $c1 = new Criteria();
     $c1->addAsColumn('foo', BookPeer::TITLE);
     $c2 = new Criteria();
     $c2->addAsColumn('bar', BookPeer::ID);
     $c1->mergeWith($c2);
     $this->assertEquals(array('foo' => BookPeer::TITLE, 'bar' => BookPeer::ID), $c1->getAsColumns(), 'mergeWith() merges the select columns after the existing as columns');
 }