Skip to content

Commit

Permalink
bug #561 [Autocomplete] Don't add WHERE IN criteria without params (n…
Browse files Browse the repository at this point in the history
…orkunas)

This PR was merged into the 2.x branch.

Discussion
----------

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

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

Commits
-------

d581ea7 [Autocomplete] Don't add WHERE IN criteria without params
  • Loading branch information
weaverryan committed Nov 28, 2022
2 parents 95f5011 + d581ea7 commit 4296071
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
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) {
$queryBuilder
->where(sprintf("o.$idField IN (%s)", implode(', ', array_keys($params))))
->setParameters(new ArrayCollection($params));
}

$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)
;
}
}

0 comments on commit 4296071

Please sign in to comment.