Download tasks/21db7eb905fcbec9/source/patch.diff from FuCY/php: direct link, hf CLI and curl.
- Browser
- Download file 4.93 kB
-
https://huggingface.co/datasets/FuCY/php/resolve/main/tasks/21db7eb905fcbec9/source/patch.diff
- Command line
-
hf download hf://datasets/FuCY/php/tasks/21db7eb905fcbec9/source/patch.diff
-
curl -L -o patch.diff https://huggingface.co/datasets/FuCY/php/resolve/main/tasks/21db7eb905fcbec9/source/patch.diff
4.93 kB
| 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 | |
| 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; | |
| 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]; | |
| 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 | |
| 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() | |
| 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') | |
| ; | |
| } | |