Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
<?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