Skip to content
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

Fix Doctrine query for nested subresources #1608

Merged
merged 7 commits into from
Dec 29, 2017

Conversation

alanpoulain
Copy link
Member

Q A
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #1442
License MIT
Doc PR

Fix the build of the Doctrine query for subresources nested in more than 2 levels of depth.
Use a recursive function instead of a while loop.

@alanpoulain alanpoulain force-pushed the fix/recursive-subresources branch 2 times, most recently from fe65701 to ac17260 Compare December 26, 2017 16:02
@alanpoulain alanpoulain force-pushed the fix/recursive-subresources branch from ac17260 to 3ef7c9e Compare December 26, 2017 16:28
@alanpoulain alanpoulain changed the base branch from master to 2.1 December 26, 2017 16:29
*
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not needed

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thought strategy="AUTO" was the default value.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh you meant that, OK!

$previousQueryBuilder = $qb;
} else {
$previousQueryBuilder->andWhere($qb->expr()->in($previousAlias, $qb->getDQL()));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is recursive already no? Not sure what was going on wrong here 😸

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this was not recursive (despite the comment!).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should've been ? 😄

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup! :D

@alanpoulain alanpoulain force-pushed the fix/recursive-subresources branch from 15f4dd0 to 3ef7c9e Compare December 26, 2017 16:48
*/
private function buildQuery(array $identifiers, array $context, QueryNameGenerator $queryNameGenerator, QueryBuilder $previousQueryBuilder, string $previousAlias, int $remainingIdentifiers, QueryBuilder $topQueryBuilder = null): QueryBuilder
{
if (!$remainingIdentifiers) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0 < --$remainingIdentifiers then we can avoid -- below and the $remainingIdentifiers - 1 operation?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's clearer this way. Because if -- is done here it's more the position of the identifier and not the remaining ones. And you have to add the operation $remainingIdentifiers + 1 where there is $remainingIdentifiers. WDYT?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok then maybe 0 === $remainingIdentifiers instead of !?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you prefer. Then not 0 >= $remainingIdentifiers?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or even $remainingIdentifiers <= 0 (I don't really like Yoda style for comparison)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dunno it's fine like this as well.

$qb->select($joinAlias)
->from($identifierResourceClass, $alias)
->innerJoin("$alias.$previousAssociationProperty", $joinAlias);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for cosmetics, new line is useless here imo \o/

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure :)

@dunglas dunglas merged commit a62e089 into api-platform:2.1 Dec 29, 2017
@dunglas
Copy link
Member

dunglas commented Dec 29, 2017

Thanks @alanpoulain

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants