Permalink
Browse files

Add to Criteria support for IN and NOT IN operators in JOIN conditions

e.g. JOIN x ON (x.id = y.x_id AND y.foo IN (42, 51))
  • Loading branch information...
arvenil committed Apr 13, 2013
1 parent 54362ae commit 0ad16985c5a249cb82b7c7203be76d98be32b01d
Showing with 37 additions and 8 deletions.
  1. +11 −8 runtime/lib/query/Criteria.php
  2. +26 −0 test/testsuite/runtime/query/CriteriaTest.php
@@ -954,6 +954,7 @@ public function addMultipleJoin($conditions, $joinType = null)
foreach ($conditions as $condition) {
$left = $condition[0];
$right = $condition[1];
+ $operator = isset($condition[2]) ? $condition[2] : JOIN::EQUAL;
if ($pos = strrpos($left, '.')) {
$leftTableAlias = substr($left, 0, $pos);
$leftColumnName = substr($left, $pos + 1);
@@ -962,26 +963,28 @@ public function addMultipleJoin($conditions, $joinType = null)
list($leftTableName, $leftTableAlias) = array(null, null);
$leftColumnName = $left;
}
- if ($pos = strrpos($right, '.')) {
+ if (!is_array($right) && $pos = strrpos($right, '.')) {
$rightTableAlias = substr($right, 0, $pos);
$rightColumnName = substr($right, $pos + 1);
list($rightTableName, $rightTableAlias) = $this->getTableNameAndAlias($rightTableAlias);
+ $conditionClause = $leftTableAlias ? $leftTableAlias . '.' : ($leftTableName ? $leftTableName . '.' : '');
+ $conditionClause .= $leftColumnName;
+ $conditionClause .= $operator;
+ $conditionClause .= $rightTableAlias ? $rightTableAlias . '.' : ($rightTableName ? $rightTableName . '.' : '');
+ $conditionClause .= $rightColumnName;
+ $comparison = Criteria::CUSTOM;
} else {
list($rightTableName, $rightTableAlias) = array(null, null);
- $rightColumnName = $right;
+ $conditionClause = $right;
+ $comparison = $operator;
}
if (!$join->getRightTableName()) {
$join->setRightTableName($rightTableName);
}
if (!$join->getRightTableAlias()) {
$join->setRightTableAlias($rightTableAlias);
}
- $conditionClause = $leftTableAlias ? $leftTableAlias . '.' : ($leftTableName ? $leftTableName . '.' : '');
- $conditionClause .= $leftColumnName;
- $conditionClause .= isset($condition[2]) ? $condition[2] : JOIN::EQUAL;
- $conditionClause .= $rightTableAlias ? $rightTableAlias . '.' : ($rightTableName ? $rightTableName . '.' : '');
- $conditionClause .= $rightColumnName;
- $criterion = $this->getNewCriterion($leftTableName . '.' . $leftColumnName, $conditionClause, Criteria::CUSTOM);
+ $criterion = $this->getNewCriterion($leftTableName . '.' . $leftColumnName, $conditionClause, $comparison);
if (null === $joinCondition) {
$joinCondition = $criterion;
} else {
@@ -874,6 +874,32 @@ public function testAddJoinMultipleWithJoinTypeAndOperator()
$this->assertEquals($expect, $result);
}
+ public function testAddJoinMultipleWithInOperator()
+ {
+ $con = Propel::getConnection(BookPeer::DATABASE_NAME);
+ $c = new Criteria();
+ $c->addMultipleJoin(array(
+ array(AuthorPeer::ID, BookPeer::AUTHOR_ID),
+ array(BookPeer::ISBN, array(1, 7, 42), Criteria::IN)
+ ), Criteria::LEFT_JOIN);
+ AuthorPeer::doSelectOne($c, $con);
+ $expectedSQL = 'SELECT author.id, author.first_name, author.last_name, author.email, author.age FROM author LEFT JOIN book ON (author.id=book.author_id AND book.isbn IN (1,7,42)) LIMIT 1';
+ $this->assertEquals($expectedSQL, $con->getLastExecutedQuery());
+ }
+
+ public function testAddJoinMultipleWithNotInOperator()
+ {
+ $con = Propel::getConnection(BookPeer::DATABASE_NAME);
+ $c = new Criteria();
+ $c->addMultipleJoin(array(
+ array(AuthorPeer::ID, BookPeer::AUTHOR_ID),
+ array(BookPeer::ISBN, array(1, 7, 42), Criteria::NOT_IN)
+ ), Criteria::LEFT_JOIN);
+ AuthorPeer::doSelectOne($c, $con);
+ $expectedSQL = 'SELECT author.id, author.first_name, author.last_name, author.email, author.age FROM author LEFT JOIN book ON (author.id=book.author_id AND book.isbn NOT IN (1,7,42)) LIMIT 1';
+ $this->assertEquals($expectedSQL, $con->getLastExecutedQuery());
+ }
+
/**
* Tests adding duplicate joins.
* @link http://trac.propelorm.org/ticket/613

0 comments on commit 0ad1698

Please sign in to comment.