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

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

Merged
merged 1 commit into from
Nov 28, 2022

Conversation

norkunas
Copy link
Contributor

Q A
Bug fix? yes
New feature? no
Tickets Fix #549
License MIT

if ($params) {
$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? :)

@daFish
Copy link
Contributor

daFish commented Nov 25, 2022

@norkunas Great fix. Would you mind adding a testcase?

->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.

@norkunas
Copy link
Contributor Author

@norkunas Great fix. Would you mind adding a testcase?

testcase added.

@weaverryan
Copy link
Member

Awesome, thank you Tomas!

@weaverryan weaverryan merged commit 4296071 into symfony:2.x Nov 28, 2022
@norkunas norkunas deleted the fix-autocomplete branch November 28, 2022 15:16
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.

[Autocomplete] 500 when using multiple=true on a not required field
5 participants