11
11
12
12
namespace Symfony \UX \Autocomplete \Form ;
13
13
14
- use Doctrine \Common \Collections \ArrayCollection ;
15
14
use Doctrine \ORM \EntityManagerInterface ;
16
- use Doctrine \ORM \Query \Parameter ;
17
15
use Doctrine \ORM \Utility \PersisterHelper ;
18
16
use Symfony \Bridge \Doctrine \Form \Type \EntityType ;
19
17
use Symfony \Component \EventDispatcher \EventSubscriberInterface ;
@@ -54,13 +52,15 @@ public function preSubmit(FormEvent $event)
54
52
$ form = $ event ->getForm ();
55
53
$ options = $ form ->get ('autocomplete ' )->getConfig ()->getOptions ();
56
54
55
+ /** @var EntityManagerInterface $em */
56
+ $ em = $ options ['em ' ];
57
+ $ repository = $ em ->getRepository ($ options ['class ' ]);
58
+ $ queryBuilder = $ options ['query_builder ' ] ?: $ repository ->createQueryBuilder ('o ' );
59
+ $ rootAlias = $ queryBuilder ->getRootAliases ()[0 ];
60
+
57
61
if (!isset ($ data ['autocomplete ' ]) || '' === $ data ['autocomplete ' ]) {
58
62
$ options ['choices ' ] = [];
59
63
} else {
60
- /** @var EntityManagerInterface $em */
61
- $ em = $ options ['em ' ];
62
- $ repository = $ em ->getRepository ($ options ['class ' ]);
63
-
64
64
$ idField = $ options ['id_reader ' ]->getIdField ();
65
65
$ idType = PersisterHelper::getTypeOfField ($ idField , $ em ->getClassMetadata ($ options ['class ' ]), $ em )[0 ];
66
66
@@ -69,22 +69,23 @@ public function preSubmit(FormEvent $event)
69
69
$ idx = 0 ;
70
70
71
71
foreach ($ data ['autocomplete ' ] as $ id ) {
72
- $ params [":id_ $ idx " ] = new Parameter ( " id_ $ idx " , $ id , $ idType) ;
72
+ $ params [":id_ $ idx " ] = [ $ id , $ idType] ;
73
73
++$ idx ;
74
74
}
75
75
76
- $ queryBuilder = $ repository ->createQueryBuilder ('o ' );
77
-
78
76
if ($ params ) {
79
77
$ queryBuilder
80
- ->where (sprintf ("o. $ idField IN (%s) " , implode (', ' , array_keys ($ params ))))
81
- ->setParameters (new ArrayCollection ($ params ));
78
+ ->andWhere (sprintf ("$ rootAlias. $ idField IN (%s) " , implode (', ' , array_keys ($ params ))))
79
+ ;
80
+ foreach ($ params as $ key => $ param ) {
81
+ $ queryBuilder ->setParameter ($ key , $ param [0 ], $ param [1 ]);
82
+ }
82
83
}
83
84
84
85
$ options ['choices ' ] = $ queryBuilder ->getQuery ()->getResult ();
85
86
} else {
86
- $ options ['choices ' ] = $ repository -> createQueryBuilder ( ' o ' )
87
- ->where ( " o .$ idField = :id " )
87
+ $ options ['choices ' ] = $ queryBuilder
88
+ ->andWhere ( " $ rootAlias . $ idField = :id " )
88
89
->setParameter ('id ' , $ data ['autocomplete ' ], $ idType )
89
90
->getQuery ()
90
91
->getResult ();
0 commit comments