Commit a269fcdb authored by zYne's avatar zYne

Custom join condition model rewritte, full support for ON and WITH keywords

parent 34711038
...@@ -674,7 +674,13 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable ...@@ -674,7 +674,13 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
if (isset($this->_pendingJoinConditions[$k])) { if (isset($this->_pendingJoinConditions[$k])) {
$parser = new Doctrine_Query_JoinCondition($this); $parser = new Doctrine_Query_JoinCondition($this);
$part .= ' AND ' . $parser->parse($this->_pendingJoinConditions[$k]);
if (strpos($part, ' ON ') !== false) {
$part .= ' AND ';
} else {
$part .= ' ON ';
}
$part .= $parser->parse($this->_pendingJoinConditions[$k]);
unset($this->_pendingJoinConditions[$k]); unset($this->_pendingJoinConditions[$k]);
} }
...@@ -1113,6 +1119,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable ...@@ -1113,6 +1119,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
return $this; return $this;
} }
public function load($path, $loadFields = true) public function load($path, $loadFields = true)
{ {
// parse custom join conditions // parse custom join conditions
...@@ -1120,10 +1127,19 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable ...@@ -1120,10 +1127,19 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
$joinCondition = ''; $joinCondition = '';
if (count($e) > 1) {
$joinCondition = $e[1];
$overrideJoin = true;
$path = $e[0];
} else {
$e = explode(' WITH ', $path);
if (count($e) > 1) { if (count($e) > 1) {
$joinCondition = $e[1]; $joinCondition = $e[1];
$path = $e[0]; $path = $e[0];
} }
$overrideJoin = false;
}
$tmp = explode(' ', $path); $tmp = explode(' ', $path);
$componentAlias = $originalAlias = (count($tmp) > 1) ? end($tmp) : null; $componentAlias = $originalAlias = (count($tmp) > 1) ? end($tmp) : null;
...@@ -1207,23 +1223,36 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable ...@@ -1207,23 +1223,36 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
$assocAlias = $this->getTableAlias($assocPath, $asf->getTableName()); $assocAlias = $this->getTableAlias($assocPath, $asf->getTableName());
$queryPart = $join . $assocTableName . ' ' . $assocAlias . ' ON ' . $localAlias . '.' $queryPart = $join . $assocTableName . ' ' . $assocAlias;
. $table->getIdentifier() . ' = '
$queryPart .= ' ON ' . $localAlias
. '.'
. $table->getIdentifier()
. ' = '
. $assocAlias . '.' . $relation->getLocal(); . $assocAlias . '.' . $relation->getLocal();
if ($relation->isEqual()) { if ($relation->isEqual()) {
$queryPart .= ' OR ' . $localAlias . '.' // equal nest relation needs additional condition
. $table->getIdentifier() . ' = ' $queryPart .= ' OR ' . $localAlias
. '.'
. $table->getIdentifier()
. ' = '
. $assocAlias . '.' . $relation->getForeign(); . $assocAlias . '.' . $relation->getForeign();
} }
$this->parts['from'][] = $queryPart; $this->parts['from'][] = $queryPart;
$queryPart = $join . $foreignSql . ' ON '; $queryPart = $join . $foreignSql;
if ( ! $overrideJoin) {
$queryPart .= ' ON ';
if ($relation->isEqual()) { if ($relation->isEqual()) {
$queryPart .= '('; $queryPart .= '(';
} }
$queryPart .= $this->_conn->quoteIdentifier($foreignAlias . '.' . $relation->getTable()->getIdentifier()) $queryPart .= $this->_conn->quoteIdentifier($foreignAlias . '.' . $relation->getTable()->getIdentifier())
. ' = ' . ' = '
. $this->_conn->quoteIdentifier($assocAlias . '.' . $relation->getForeign()); . $this->_conn->quoteIdentifier($assocAlias . '.' . $relation->getForeign());
...@@ -1238,14 +1267,17 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable ...@@ -1238,14 +1267,17 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
. ' != ' . ' != '
. $this->_conn->quoteIdentifier($localAlias . '.' . $table->getIdentifier()); . $this->_conn->quoteIdentifier($localAlias . '.' . $table->getIdentifier());
} }
}
} else { } else {
$queryPart = $join . $foreignSql $queryPart = $join . $foreignSql;
. ' ON '
if ( ! $overrideJoin) {
$queryPart .= ' ON '
. $this->_conn->quoteIdentifier($localAlias . '.' . $relation->getLocal()) . $this->_conn->quoteIdentifier($localAlias . '.' . $relation->getLocal())
. ' = ' . ' = '
. $this->_conn->quoteIdentifier($foreignAlias . '.' . $relation->getForeign()); . $this->_conn->quoteIdentifier($foreignAlias . '.' . $relation->getForeign());
}
} }
$this->parts['from'][$componentAlias] = $queryPart; $this->parts['from'][$componentAlias] = $queryPart;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment