Skip to content

Commit 757ed63

Browse files
Merge branch '2.7' into 2.8
* 2.7: [Process] Fix running tests on HHVM>=3.8 [Form] Improved performance of ChoiceType and its subtypes
2 parents a1f65cd + 68aa9c0 commit 757ed63

File tree

3 files changed

+84
-3
lines changed

3 files changed

+84
-3
lines changed

Form/Type/DoctrineType.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,12 @@ public function getQueryBuilderPartsForCachingHash($queryBuilder)
111111
public function __construct(ManagerRegistry $registry, PropertyAccessorInterface $propertyAccessor = null, ChoiceListFactoryInterface $choiceListFactory = null)
112112
{
113113
$this->registry = $registry;
114-
$this->choiceListFactory = $choiceListFactory ?: new PropertyAccessDecorator(new DefaultChoiceListFactory(), $propertyAccessor);
114+
$this->choiceListFactory = $choiceListFactory ?: new CachingFactoryDecorator(
115+
new PropertyAccessDecorator(
116+
new DefaultChoiceListFactory(),
117+
$propertyAccessor
118+
)
119+
);
115120
}
116121

117122
public function buildForm(FormBuilderInterface $builder, array $options)

Form/Type/EntityType.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bridge\Doctrine\Form\Type;
1313

1414
use Doctrine\Common\Persistence\ObjectManager;
15+
use Doctrine\ORM\Query\Parameter;
1516
use Doctrine\ORM\QueryBuilder;
1617
use Symfony\Bridge\Doctrine\Form\ChoiceList\ORMQueryBuilderLoader;
1718
use Symfony\Component\Form\Exception\UnexpectedTypeException;
@@ -86,8 +87,20 @@ public function getBlockPrefix()
8687
public function getQueryBuilderPartsForCachingHash($queryBuilder)
8788
{
8889
return array(
89-
$queryBuilder->getQuery()->getSQL(),
90-
$queryBuilder->getParameters()->toArray(),
90+
$queryBuilder->getQuery()->getSQL(),
91+
array_map(array($this, 'parameterToArray'), $queryBuilder->getParameters()->toArray()),
9192
);
9293
}
94+
95+
/**
96+
* Converts a query parameter to an array.
97+
*
98+
* @param Parameter $parameter The query parameter
99+
*
100+
* @return array The array representation of the parameter
101+
*/
102+
private function parameterToArray(Parameter $parameter)
103+
{
104+
return array($parameter->getName(), $parameter->getType(), $parameter->getValue());
105+
}
93106
}

Tests/Form/Type/EntityTypeTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,6 +1140,69 @@ public function testLoaderCaching()
11401140
$this->assertSame($choiceLoader1, $choiceLoader3);
11411141
}
11421142

1143+
public function testLoaderCachingWithParameters()
1144+
{
1145+
$entity1 = new SingleIntIdEntity(1, 'Foo');
1146+
$entity2 = new SingleIntIdEntity(2, 'Bar');
1147+
$entity3 = new SingleIntIdEntity(3, 'Baz');
1148+
1149+
$this->persist(array($entity1, $entity2, $entity3));
1150+
1151+
$repo = $this->em->getRepository(self::SINGLE_IDENT_CLASS);
1152+
1153+
$entityType = new EntityType(
1154+
$this->emRegistry,
1155+
PropertyAccess::createPropertyAccessor()
1156+
);
1157+
1158+
$entityTypeGuesser = new DoctrineOrmTypeGuesser($this->emRegistry);
1159+
1160+
$factory = Forms::createFormFactoryBuilder()
1161+
->addType($entityType)
1162+
->addTypeGuesser($entityTypeGuesser)
1163+
->getFormFactory();
1164+
1165+
$formBuilder = $factory->createNamedBuilder('form', 'form');
1166+
1167+
$formBuilder->add('property1', 'entity', array(
1168+
'em' => 'default',
1169+
'class' => self::SINGLE_IDENT_CLASS,
1170+
'query_builder' => $repo->createQueryBuilder('e')->where('e.id = :id')->setParameter('id', 1),
1171+
));
1172+
1173+
$formBuilder->add('property2', 'entity', array(
1174+
'em' => 'default',
1175+
'class' => self::SINGLE_IDENT_CLASS,
1176+
'query_builder' => function (EntityRepository $repo) {
1177+
return $repo->createQueryBuilder('e')->where('e.id = :id')->setParameter('id', 1);
1178+
},
1179+
));
1180+
1181+
$formBuilder->add('property3', 'entity', array(
1182+
'em' => 'default',
1183+
'class' => self::SINGLE_IDENT_CLASS,
1184+
'query_builder' => function (EntityRepository $repo) {
1185+
return $repo->createQueryBuilder('e')->where('e.id = :id')->setParameter('id', 1);
1186+
},
1187+
));
1188+
1189+
$form = $formBuilder->getForm();
1190+
1191+
$form->submit(array(
1192+
'property1' => 1,
1193+
'property2' => 1,
1194+
'property3' => 2,
1195+
));
1196+
1197+
$choiceList1 = $form->get('property1')->getConfig()->getOption('choice_list');
1198+
$choiceList2 = $form->get('property2')->getConfig()->getOption('choice_list');
1199+
$choiceList3 = $form->get('property3')->getConfig()->getOption('choice_list');
1200+
1201+
$this->assertInstanceOf('Symfony\Component\Form\ChoiceList\ChoiceListInterface', $choiceList1);
1202+
$this->assertSame($choiceList1, $choiceList2);
1203+
$this->assertSame($choiceList1, $choiceList3);
1204+
}
1205+
11431206
public function testCacheChoiceLists()
11441207
{
11451208
$entity1 = new SingleIntIdEntity(1, 'Foo');

0 commit comments

Comments
 (0)