Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1487 from soyuka/merge-2.1
Merge 2.1
- Loading branch information
Showing
15 changed files
with
308 additions
and
96 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Core\Bridge\Doctrine\Orm\Util; | ||
|
||
use Doctrine\ORM\Query\Expr\Join; | ||
use Doctrine\ORM\QueryBuilder; | ||
|
||
/** | ||
* @author Vincent Chalamon <vincent@les-tilleuls.coop> | ||
* | ||
* @internal | ||
*/ | ||
final class QueryBuilderHelper | ||
{ | ||
private function __construct() | ||
{ | ||
} | ||
|
||
/** | ||
* Adds a join to the queryBuilder if none exists. | ||
*/ | ||
public static function addJoinOnce(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $alias, string $association, string $joinType = null, string $conditionType = null, string $condition = null): string | ||
{ | ||
$join = self::getExistingJoin($queryBuilder, $alias, $association); | ||
|
||
if (null !== $join) { | ||
return $join->getAlias(); | ||
} | ||
|
||
$associationAlias = $queryNameGenerator->generateJoinAlias($association); | ||
$query = "$alias.$association"; | ||
|
||
if (Join::LEFT_JOIN === $joinType || QueryChecker::hasLeftJoin($queryBuilder)) { | ||
$queryBuilder->leftJoin($query, $associationAlias, $conditionType, $condition); | ||
} else { | ||
$queryBuilder->innerJoin($query, $associationAlias, $conditionType, $condition); | ||
} | ||
|
||
return $associationAlias; | ||
} | ||
|
||
/** | ||
* Get the existing join from queryBuilder DQL parts. | ||
* | ||
* @return Join|null | ||
*/ | ||
private static function getExistingJoin(QueryBuilder $queryBuilder, string $alias, string $association) | ||
{ | ||
$parts = $queryBuilder->getDQLPart('join'); | ||
|
||
if (!isset($parts['o'])) { | ||
return null; | ||
} | ||
|
||
foreach ($parts['o'] as $join) { | ||
/** @var Join $join */ | ||
if (sprintf('%s.%s', $alias, $association) === $join->getJoin()) { | ||
return $join; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} |
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
Oops, something went wrong.