Skip to content

Commit b9ec99e

Browse files
Merge branch '2.8' into 3.0
* 2.8: Fix merge [Process] Fix running tests on HHVM>=3.8 [Form] Improved performance of ChoiceType and its subtypes Removed an object as route generator argument Conflicts: src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypeTest.php src/Symfony/Bundle/FrameworkBundle/Resources/config/form.xml
2 parents dc8b86b + 95668dc commit b9ec99e

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;
@@ -78,8 +79,20 @@ public function getBlockPrefix()
7879
public function getQueryBuilderPartsForCachingHash($queryBuilder)
7980
{
8081
return array(
81-
$queryBuilder->getQuery()->getSQL(),
82-
$queryBuilder->getParameters()->toArray(),
82+
$queryBuilder->getQuery()->getSQL(),
83+
array_map(array($this, 'parameterToArray'), $queryBuilder->getParameters()->toArray()),
8384
);
8485
}
86+
87+
/**
88+
* Converts a query parameter to an array.
89+
*
90+
* @param Parameter $parameter The query parameter
91+
*
92+
* @return array The array representation of the parameter
93+
*/
94+
private function parameterToArray(Parameter $parameter)
95+
{
96+
return array($parameter->getName(), $parameter->getType(), $parameter->getValue());
97+
}
8598
}

Tests/Form/Type/EntityTypeTest.php

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

1130+
public function testLoaderCachingWithParameters()
1131+
{
1132+
$entity1 = new SingleIntIdEntity(1, 'Foo');
1133+
$entity2 = new SingleIntIdEntity(2, 'Bar');
1134+
$entity3 = new SingleIntIdEntity(3, 'Baz');
1135+
1136+
$this->persist(array($entity1, $entity2, $entity3));
1137+
1138+
$repo = $this->em->getRepository(self::SINGLE_IDENT_CLASS);
1139+
1140+
$entityType = new EntityType(
1141+
$this->emRegistry,
1142+
PropertyAccess::createPropertyAccessor()
1143+
);
1144+
1145+
$entityTypeGuesser = new DoctrineOrmTypeGuesser($this->emRegistry);
1146+
1147+
$factory = Forms::createFormFactoryBuilder()
1148+
->addType($entityType)
1149+
->addTypeGuesser($entityTypeGuesser)
1150+
->getFormFactory();
1151+
1152+
$formBuilder = $factory->createNamedBuilder('form', 'Symfony\Component\Form\Extension\Core\Type\FormType');
1153+
1154+
$formBuilder->add('property1', 'Symfony\Bridge\Doctrine\Form\Type\EntityType', array(
1155+
'em' => 'default',
1156+
'class' => self::SINGLE_IDENT_CLASS,
1157+
'query_builder' => $repo->createQueryBuilder('e')->where('e.id = :id')->setParameter('id', 1),
1158+
));
1159+
1160+
$formBuilder->add('property2', 'Symfony\Bridge\Doctrine\Form\Type\EntityType', array(
1161+
'em' => 'default',
1162+
'class' => self::SINGLE_IDENT_CLASS,
1163+
'query_builder' => function (EntityRepository $repo) {
1164+
return $repo->createQueryBuilder('e')->where('e.id = :id')->setParameter('id', 1);
1165+
},
1166+
));
1167+
1168+
$formBuilder->add('property3', 'Symfony\Bridge\Doctrine\Form\Type\EntityType', array(
1169+
'em' => 'default',
1170+
'class' => self::SINGLE_IDENT_CLASS,
1171+
'query_builder' => function (EntityRepository $repo) {
1172+
return $repo->createQueryBuilder('e')->where('e.id = :id')->setParameter('id', 1);
1173+
},
1174+
));
1175+
1176+
$form = $formBuilder->getForm();
1177+
1178+
$form->submit(array(
1179+
'property1' => 1,
1180+
'property2' => 1,
1181+
'property3' => 2,
1182+
));
1183+
1184+
$choiceLoader1 = $form->get('property1')->getConfig()->getOption('choice_loader');
1185+
$choiceLoader2 = $form->get('property2')->getConfig()->getOption('choice_loader');
1186+
$choiceLoader3 = $form->get('property3')->getConfig()->getOption('choice_loader');
1187+
1188+
$this->assertInstanceOf('Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface', $choiceLoader1);
1189+
$this->assertSame($choiceLoader1, $choiceLoader2);
1190+
$this->assertSame($choiceLoader1, $choiceLoader3);
1191+
}
1192+
11301193
protected function createRegistryMock($name, $em)
11311194
{
11321195
$registry = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry');

0 commit comments

Comments
 (0)