Created
December 6, 2011 02:58
-
-
Save khepin/1436493 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class RealmWalker extends \Doctrine\ORM\Query\TreeWalkerAdapter | |
{ | |
public function walkSelectStatement(\Doctrine\ORM\Query\AST\SelectStatement $AST) | |
{ | |
$parent = null; | |
$parentName = null; | |
foreach ($this->_getQueryComponents() AS $dqlAlias => $qComp) { | |
// skip mixed data in query | |
if (isset($qComp['resultVariable'])) { | |
continue; | |
} | |
if ($qComp['parent'] === null && $qComp['nestingLevel'] == 0) { | |
$parent = $qComp; | |
$parentName = $dqlAlias; | |
break; | |
} | |
} | |
$field = $parent['metadata']->getFieldForColumn($this->_getQuery()->getHint('khepin.realm.field')); | |
$pathExpression = new PathExpression( | |
PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION, $parentName, $field | |
); | |
$pathExpression->type = PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION; | |
$compExpression = new ComparisonExpression($pathExpression, '=', $this->_getQuery()->getHint('khepin.realm.id')); | |
$conditionalPrimary = new ConditionalPrimary(); | |
$conditionalPrimary->simpleConditionalExpression = $compExpression; | |
// if no existing whereClause | |
if ($AST->whereClause === null) { | |
$AST->whereClause = new WhereClause( | |
new ConditionalExpression(array( | |
new ConditionalTerm(array( | |
new ConditionalFactor($conditionalPrimary) | |
)) | |
)) | |
); | |
} else { // add to the existing using AND | |
// existing AND clause | |
if ($AST->whereClause->conditionalExpression instanceof ConditionalTerm) { | |
$AST->whereClause->conditionalExpression->conditionalFactors[] = $conditionalPrimary; | |
} | |
// single clause where | |
elseif ($AST->whereClause->conditionalExpression instanceof ConditionalPrimary) { | |
$AST->whereClause->conditionalExpression = new ConditionalExpression( | |
array( | |
new ConditionalTerm( | |
array( | |
$AST->whereClause->conditionalExpression, | |
$conditionalPrimary | |
) | |
) | |
) | |
); | |
} | |
// an OR clause | |
elseif ($AST->whereClause->conditionalExpression instanceof ConditionalExpression) { | |
$tmpPrimary = new ConditionalPrimary; | |
$tmpPrimary->conditionalExpression = $AST->whereClause->conditionalExpression; | |
$AST->whereClause->conditionalExpression = new ConditionalTerm( | |
array( | |
$tmpPrimary, | |
$conditionalPrimary, | |
) | |
); | |
} else { | |
// error check to provide a more verbose error on failure | |
throw \Exception("Unknown conditionalExpression in RealmWalker"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment