-
-
Notifications
You must be signed in to change notification settings - Fork 921
[RFR] Issues/1246 #1324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[RFR] Issues/1246 #1324
Changes from all commits
159572b
93ac012
b8eb3a0
bb8dc16
09b6cfb
82218e8
b42e46f
c82dd82
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 <[email protected]> | ||
* | ||
* 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 <[email protected]> | ||
* | ||
* @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'])) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we could put this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It could be easily retried by There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. don't IMO it's fine like this. It's just that we have a lot of these |
||
return null; | ||
} | ||
|
||
foreach ($parts['o'] as $join) { | ||
/** @var Join $join */ | ||
if (sprintf('%s.%s', $alias, $association) === $join->getJoin()) { | ||
return $join; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -269,8 +269,8 @@ public function testCompositeIdentifiers() | |
->setParameter('foo', 1); | ||
|
||
$queryNameGenerator = $this->prophesize(QueryNameGeneratorInterface::class); | ||
$queryNameGenerator->generateJoinAlias('item')->shouldBeCalled()->willReturn('item_2'); | ||
$queryNameGenerator->generateJoinAlias('label')->shouldBeCalled()->willReturn('label_2'); | ||
$queryNameGenerator->generateJoinAlias('compositeItem')->shouldBeCalled()->willReturn('item_2'); | ||
$queryNameGenerator->generateJoinAlias('compositeLabel')->shouldBeCalled()->willReturn('label_2'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change is wrong, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nop, cause this call is made through QueryBuilderHelper::addJoinOnce from association name. I don't understand why travis is failing…on a non-existing unit test 😮 (failing method There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll try to squash my changes to force travis to rebuild its environment There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added this recently maybe a bad rebase? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rebase missing, I fixed it, thanks @soyuka |
||
$queryNameGenerator->generateJoinAlias('o')->shouldBeCalled()->willReturn('o_2'); | ||
|
||
$filterEagerLoadingExtension = new FilterEagerLoadingExtension($resourceMetadataFactoryProphecy->reveal(), true); | ||
|
@@ -325,8 +325,8 @@ public function testFetchEagerWithNoForceEager() | |
->setParameter('foo', 1); | ||
|
||
$queryNameGenerator = $this->prophesize(QueryNameGeneratorInterface::class); | ||
$queryNameGenerator->generateJoinAlias('item')->shouldBeCalled()->willReturn('item_2'); | ||
$queryNameGenerator->generateJoinAlias('label')->shouldBeCalled()->willReturn('label_2'); | ||
$queryNameGenerator->generateJoinAlias('compositeItem')->shouldBeCalled()->willReturn('item_2'); | ||
$queryNameGenerator->generateJoinAlias('compositeLabel')->shouldBeCalled()->willReturn('label_2'); | ||
$queryNameGenerator->generateJoinAlias('o')->shouldBeCalled()->willReturn('o_2'); | ||
|
||
$queryNameGenerator->generateJoinAlias('foo')->shouldBeCalled()->willReturn('foo_2'); | ||
|
@@ -388,8 +388,8 @@ public function testCompositeIdentifiersWithoutAssociation() | |
->setParameter('bar', 2); | ||
|
||
$queryNameGenerator = $this->prophesize(QueryNameGeneratorInterface::class); | ||
$queryNameGenerator->generateJoinAlias('item')->shouldBeCalled()->willReturn('item_2'); | ||
$queryNameGenerator->generateJoinAlias('label')->shouldBeCalled()->willReturn('label_2'); | ||
$queryNameGenerator->generateJoinAlias('compositeItem')->shouldBeCalled()->willReturn('item_2'); | ||
$queryNameGenerator->generateJoinAlias('compositeLabel')->shouldBeCalled()->willReturn('label_2'); | ||
$queryNameGenerator->generateJoinAlias('o')->shouldBeCalled()->willReturn('o_2'); | ||
|
||
$filterEagerLoadingExtension = new FilterEagerLoadingExtension($resourceMetadataFactoryProphecy->reveal(), true); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a private constructor?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why for?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because he likes private constructors :P.
Nah just that it's a convention when classes have no instances (ie are never called with
new
because they only have statics).