File size: 4,933 Bytes
4d2a9bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
diff --git a/src/Form/AutocompleteEntityTypeSubscriber.php b/src/Form/AutocompleteEntityTypeSubscriber.php
index 4349dc3..3b774b9 100644
--- a/src/Form/AutocompleteEntityTypeSubscriber.php
+++ b/src/Form/AutocompleteEntityTypeSubscriber.php
@@ -11,9 +11,7 @@
 
 namespace Symfony\UX\Autocomplete\Form;
 
-use Doctrine\Common\Collections\ArrayCollection;
 use Doctrine\ORM\EntityManagerInterface;
-use Doctrine\ORM\Query\Parameter;
 use Doctrine\ORM\Utility\PersisterHelper;
 use Symfony\Bridge\Doctrine\Form\Type\EntityType;
 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
@@ -54,13 +52,15 @@ public function preSubmit(FormEvent $event)
         $form = $event->getForm();
         $options = $form->get('autocomplete')->getConfig()->getOptions();
 
+        /** @var EntityManagerInterface $em */
+        $em = $options['em'];
+        $repository = $em->getRepository($options['class']);
+        $queryBuilder = $options['query_builder'] ?: $repository->createQueryBuilder('o');
+        $rootAlias = $queryBuilder->getRootAliases()[0];
+
         if (!isset($data['autocomplete']) || '' === $data['autocomplete']) {
             $options['choices'] = [];
         } else {
-            /** @var EntityManagerInterface $em */
-            $em = $options['em'];
-            $repository = $em->getRepository($options['class']);
-
             $idField = $options['id_reader']->getIdField();
             $idType = PersisterHelper::getTypeOfField($idField, $em->getClassMetadata($options['class']), $em)[0];
 
@@ -69,22 +69,23 @@ public function preSubmit(FormEvent $event)
                 $idx = 0;
 
                 foreach ($data['autocomplete'] as $id) {
-                    $params[":id_$idx"] = new Parameter("id_$idx", $id, $idType);
+                    $params[":id_$idx"] = [$id, $idType];
                     ++$idx;
                 }
 
-                $queryBuilder = $repository->createQueryBuilder('o');
-
                 if ($params) {
                     $queryBuilder
-                        ->where(sprintf("o.$idField IN (%s)", implode(', ', array_keys($params))))
-                        ->setParameters(new ArrayCollection($params));
+                        ->andWhere(sprintf("$rootAlias.$idField IN (%s)", implode(', ', array_keys($params))))
+                    ;
+                    foreach ($params as $key => $param) {
+                        $queryBuilder->setParameter($key, $param[0], $param[1]);
+                    }
                 }
 
                 $options['choices'] = $queryBuilder->getQuery()->getResult();
             } else {
-                $options['choices'] = $repository->createQueryBuilder('o')
-                    ->where("o.$idField = :id")
+                $options['choices'] = $queryBuilder
+                    ->andWhere("$rootAlias.$idField = :id")
                     ->setParameter('id', $data['autocomplete'], $idType)
                     ->getQuery()
                     ->getResult();
diff --git a/tests/Functional/AutocompleteFormRenderingTest.php b/tests/Functional/AutocompleteFormRenderingTest.php
index 0e8c2da..7cbef51 100644
--- a/tests/Functional/AutocompleteFormRenderingTest.php
+++ b/tests/Functional/AutocompleteFormRenderingTest.php
@@ -46,6 +46,7 @@ public function testCategoryFieldSubmitsCorrectly()
         $firstCat = CategoryFactory::createOne(['name' => 'First cat']);
         CategoryFactory::createOne(['name' => 'in space']);
         CategoryFactory::createOne(['name' => 'ate pizza']);
+        $fooCat = CategoryFactory::createOne(['name' => 'I contain "foo" which CategoryAutocompleteType uses its query_builder option.']);
 
         $this->browser()
             ->throwExceptions()
@@ -53,14 +54,26 @@ public function testCategoryFieldSubmitsCorrectly()
             // the field renders empty (but the placeholder is there)
             ->assertElementCount('#product_category_autocomplete option', 1)
             ->assertNotContains('First cat')
+
             ->post('/test-form', [
                 'body' => [
                     'product' => ['category' => ['autocomplete' => $firstCat->getId()]],
                 ],
             ])
+            // the option does NOT match something returned by query_builder
+            // so ONLY the placeholder shows up
+            ->assertElementCount('#product_category_autocomplete option', 1)
+            ->assertNotContains('First cat')
+
+            ->assertNotContains('First cat')
+            ->post('/test-form', [
+                'body' => [
+                    'product' => ['category' => ['autocomplete' => $fooCat->getId()]],
+                ],
+            ])
             // the one option + placeholder now shows up
             ->assertElementCount('#product_category_autocomplete option', 2)
-            ->assertContains('First cat')
+            ->assertContains('which CategoryAutocompleteType uses')
         ;
     }