Last active
May 5, 2019 00:19
-
-
Save ismail1432/d023203a56d0c6231c3338adac83f2e0 to your computer and use it in GitHub Desktop.
Fix-QueryBuilder alias Bug with custom filter https://github.com/api-platform/api-platform/issues/1128
This file contains hidden or 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 | |
final class FilterEagerLoadingExtension | |
{ | |
// ... | |
private function getQueryBuilderWithNewAliases(QueryBuilder $queryBuilder,QueryNameGeneratorInterface $queryNameGenerator,string $originAlias = 'o', string $replacement = 'o_2') | |
{ | |
// ... | |
// Change join aliases | |
foreach ($joinParts[$originAlias] as $joinPart) { | |
/** @var Join $joinPart */ | |
$joinString = str_replace($aliases, $replacements, $joinPart->getJoin()); | |
$pos = strpos($joinString, '.'); | |
if (false === $pos) { | |
continue; | |
} | |
$alias = substr($joinString, 0, $pos); | |
$association = substr($joinString, $pos + 1); | |
$condition = str_replace($aliases, $replacements, $joinPart->getCondition()); | |
$newAlias = QueryBuilderHelper::addJoinOnce($queryBuilderClone, $queryNameGenerator, $alias, $association, $joinPart->getJoinType(), $joinPart->getConditionType(), $condition, $originAlias); | |
$aliases[] = "{$joinPart->getAlias()}."; | |
$replacements[] = "$newAlias."; | |
} | |
// Added | |
$this->replaceAliasesInWherePart($wherePart, $aliases, $replacements); | |
// Added | |
$queryBuilderClone->add('where', (string) $wherePart); | |
// Removed | |
$queryBuilderClone->add('where', str_replace($aliases, $replacements, (string) $wherePart)); | |
return $queryBuilderClone; | |
} | |
/** | |
* Replace aliases in the WherePart to avoid changement of searched value. | |
* | |
* @param $wherePart | |
* @param string $originAlias the base alias | |
* @param string $replacement the replacement for the base alias, will change the from alias | |
*/ | |
private function replaceAliasesInWherePart($wherePart, $aliases, $replacements) | |
{ | |
foreach($wherePart->getParts() as $parts) { | |
if(!is_string($parts)) { | |
foreach ($parts->getParts() as $part) { | |
$whereAlias = str_replace($aliases, $replacements, $part->getLeftExpr()); | |
$part->__construct($whereAlias,$part->getOperator(),$part->getRightExpr()); | |
} | |
} | |
} | |
} | |
} | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment