Skip to content

[Autocomplete] Don't add WHERE IN criteria without params #561

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

Merged
merged 1 commit into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/Autocomplete/src/Form/AutocompleteEntityTypeSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,15 @@ public function preSubmit(FormEvent $event)
++$idx;
}

$options['choices'] = $repository->createQueryBuilder('o')
->where(sprintf("o.$idField IN (%s)", implode(', ', array_keys($params))))
->setParameters(new ArrayCollection($params))
->getQuery()
->getResult();
$queryBuilder = $repository->createQueryBuilder('o');

if ($params) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why don't just check if $data['autocomplete'] is [] and skip this else part?
Btw this can be simplified a lot like $repository->createQueryBuilder('o')->->where(sprintf('o.%s IN (:ids)', $idField)))->setParameter('ids', $data['autocomplete'])->getQuery()->getResult(), doctrine will handle the rest, doesn't it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No it won't handle the types properly,that's what I've fixed before.

$queryBuilder
->where(sprintf("o.$idField IN (%s)", implode(', ', array_keys($params))))
->setParameters(new ArrayCollection($params));
Copy link
Member

Choose a reason for hiding this comment

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

My two cents, using ->setParameters() will override the previous bound parameters (if any) to the QueryBuilder.

IMO it would be preferable to use ->setParameter() for each $params, to be future-proof.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There can't be any previous parameters, because the query is constructed 4 lines before :)

Copy link
Member

Choose a reason for hiding this comment

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

For the moment yes, but we can't predict the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Imho they could be added to $params,no? :)

}

$options['choices'] = $queryBuilder->getQuery()->getResult();
} else {
$options['choices'] = $repository->createQueryBuilder('o')
->where("o.$idField = :id")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,25 @@ public function testProperlyLoadsChoicesWithIdValueObjects()
->assertContains('Sugar')
;
}

public function testMultipleDoesNotFailWithoutSelectedChoices()
{
$this->browser()
->throwExceptions()
->get('/test-form')
->assertElementCount('#product_ingredients_autocomplete option', 0)
->assertNotContains('Flour')
->assertNotContains('Sugar')
->post('/test-form', [
'body' => [
'product' => [
'ingredients' => [
'autocomplete' => [],
],
],
],
])
->assertElementCount('#product_ingredients_autocomplete option', 0)
;
}
}