Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0c33d97

Browse files
committed
[Form] Improved performance of ChoiceType and its subtypes
1 parent77f4b23 commit0c33d97

File tree

5 files changed

+114
-14
lines changed

5 files changed

+114
-14
lines changed

‎src/Symfony/Bridge/Doctrine/Form/Type/DoctrineType.php‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,19 @@ abstract class DoctrineType extends AbstractType
3737
protected$registry;
3838

3939
/**
40-
* @varChoiceListFactoryInterface
40+
* @varDoctrineChoiceLoader[]
4141
*/
42-
private$choiceListFactory;
42+
private$choiceLoaders =array();
4343

4444
/**
4545
* @var IdReader[]
4646
*/
4747
private$idReaders =array();
4848

4949
/**
50-
* @varDoctrineChoiceLoader[]
50+
* @varChoiceListFactoryInterface
5151
*/
52-
private$choiceLoaders =array();
52+
private$choiceListFactory;
5353

5454
/**
5555
* Creates the label for a choice.
@@ -94,12 +94,12 @@ public static function createChoiceName($choice, $key, $value)
9494
* Gets important parts from QueryBuilder that will allow to cache its results.
9595
* For instance in ORM two query builders with an equal SQL string and
9696
* equal parameters are considered to be equal.
97-
*
97+
*
9898
* @param object $queryBuilder
99-
*
99+
*
100100
* @return array|false Array with important QueryBuilder parts or false if
101101
* they can't be determined
102-
*
102+
*
103103
* @internal This method is public to be usable as callback. It should not
104104
* be used in user code.
105105
*/
@@ -111,7 +111,12 @@ public function getQueryBuilderPartsForCachingHash($queryBuilder)
111111
publicfunction__construct(ManagerRegistry$registry,PropertyAccessorInterface$propertyAccessor =null,ChoiceListFactoryInterface$choiceListFactory =null)
112112
{
113113
$this->registry =$registry;
114-
$this->choiceListFactory =$choiceListFactory ?:newPropertyAccessDecorator(newDefaultChoiceListFactory(),$propertyAccessor);
114+
$this->choiceListFactory =$choiceListFactory ?:newCachingFactoryDecorator(
115+
newPropertyAccessDecorator(
116+
newDefaultChoiceListFactory(),
117+
$propertyAccessor
118+
)
119+
);
115120
}
116121

117122
publicfunctionbuildForm(FormBuilderInterface$builder,array$options)

‎src/Symfony/Bridge/Doctrine/Form/Type/EntityType.php‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespaceSymfony\Bridge\Doctrine\Form\Type;
1313

1414
useDoctrine\Common\Persistence\ObjectManager;
15+
useDoctrine\ORM\Query\Parameter;
1516
useDoctrine\ORM\QueryBuilder;
1617
useSymfony\Bridge\Doctrine\Form\ChoiceList\ORMQueryBuilderLoader;
1718
useSymfony\Component\Form\Exception\UnexpectedTypeException;
@@ -64,19 +65,31 @@ public function getName()
6465
/**
6566
* We consider two query builders with an equal SQL string and
6667
* equal parameters to be equal.
67-
*
68+
*
6869
* @param QueryBuilder $queryBuilder
69-
*
70+
*
7071
* @return array
71-
*
72+
*
7273
* @internal This method is public to be usable as callback. It should not
7374
* be used in user code.
7475
*/
7576
publicfunctiongetQueryBuilderPartsForCachingHash($queryBuilder)
7677
{
7778
returnarray(
78-
$queryBuilder->getQuery()->getSQL(),
79-
$queryBuilder->getParameters()->toArray(),
79+
$queryBuilder->getQuery()->getSQL(),
80+
array_map(array($this,'parameterToArray'),$queryBuilder->getParameters()->toArray()),
8081
);
8182
}
83+
84+
/**
85+
* Converts a query parameter to an array.
86+
*
87+
* @param Parameter $parameter The query parameter
88+
*
89+
* @return array The array representation of the parameter
90+
*/
91+
privatefunctionparameterToArray(Parameter$parameter)
92+
{
93+
returnarray($parameter->getName(),$parameter->getType(),$parameter->getValue());
94+
}
8295
}

‎src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypeTest.php‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,6 +1114,69 @@ public function testLoaderCaching()
11141114
$this->assertSame($choiceList1,$choiceList3);
11151115
}
11161116

1117+
publicfunctiontestLoaderCachingWithParameters()
1118+
{
1119+
$entity1 =newSingleIntIdEntity(1,'Foo');
1120+
$entity2 =newSingleIntIdEntity(2,'Bar');
1121+
$entity3 =newSingleIntIdEntity(3,'Baz');
1122+
1123+
$this->persist(array($entity1,$entity2,$entity3));
1124+
1125+
$repo =$this->em->getRepository(self::SINGLE_IDENT_CLASS);
1126+
1127+
$entityType =newEntityType(
1128+
$this->emRegistry,
1129+
PropertyAccess::createPropertyAccessor()
1130+
);
1131+
1132+
$entityTypeGuesser =newDoctrineOrmTypeGuesser($this->emRegistry);
1133+
1134+
$factory = Forms::createFormFactoryBuilder()
1135+
->addType($entityType)
1136+
->addTypeGuesser($entityTypeGuesser)
1137+
->getFormFactory();
1138+
1139+
$formBuilder =$factory->createNamedBuilder('form','form');
1140+
1141+
$formBuilder->add('property1','entity',array(
1142+
'em' =>'default',
1143+
'class' =>self::SINGLE_IDENT_CLASS,
1144+
'query_builder' =>$repo->createQueryBuilder('e')->where('e.id = :id')->setParameter('id',1),
1145+
));
1146+
1147+
$formBuilder->add('property2','entity',array(
1148+
'em' =>'default',
1149+
'class' =>self::SINGLE_IDENT_CLASS,
1150+
'query_builder' =>function (EntityRepository$repo) {
1151+
return$repo->createQueryBuilder('e')->where('e.id = :id')->setParameter('id',1);
1152+
},
1153+
));
1154+
1155+
$formBuilder->add('property3','entity',array(
1156+
'em' =>'default',
1157+
'class' =>self::SINGLE_IDENT_CLASS,
1158+
'query_builder' =>function (EntityRepository$repo) {
1159+
return$repo->createQueryBuilder('e')->where('e.id = :id')->setParameter('id',1);
1160+
},
1161+
));
1162+
1163+
$form =$formBuilder->getForm();
1164+
1165+
$form->submit(array(
1166+
'property1' =>1,
1167+
'property2' =>1,
1168+
'property3' =>2,
1169+
));
1170+
1171+
$choiceList1 =$form->get('property1')->getConfig()->getOption('choice_list');
1172+
$choiceList2 =$form->get('property2')->getConfig()->getOption('choice_list');
1173+
$choiceList3 =$form->get('property3')->getConfig()->getOption('choice_list');
1174+
1175+
$this->assertInstanceOf('Symfony\Component\Form\ChoiceList\ChoiceListInterface',$choiceList1);
1176+
$this->assertSame($choiceList1,$choiceList2);
1177+
$this->assertSame($choiceList1,$choiceList3);
1178+
}
1179+
11171180
publicfunctiontestCacheChoiceLists()
11181181
{
11191182
$entity1 =newSingleIntIdEntity(1,'Foo');

‎src/Symfony/Bundle/FrameworkBundle/Resources/config/form.xml‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,19 @@
5757
<!-- CoreExtension-->
5858
<serviceid="form.property_accessor"alias="property_accessor"public="false" />
5959

60+
<serviceid="form.choice_list_factory.default"class="Symfony\Component\Form\ChoiceList\Factory\DefaultChoiceListFactory"public="false"/>
61+
62+
<serviceid="form.choice_list_factory.property_access"class="Symfony\Component\Form\ChoiceList\Factory\PropertyAccessDecorator"public="false">
63+
<argumenttype="service"id="form.choice_list_factory.default"/>
64+
<argumenttype="service"id="form.property_accessor"/>
65+
</service>
66+
67+
<serviceid="form.choice_list_factory.cached"class="Symfony\Component\Form\ChoiceList\Factory\CachingFactoryDecorator"public="false">
68+
<argumenttype="service"id="form.choice_list_factory.property_access"/>
69+
</service>
70+
71+
<serviceid="form.choice_list_factory"alias="form.choice_list_factory.cached"public="false"/>
72+
6073
<serviceid="form.type.form"class="Symfony\Component\Form\Extension\Core\Type\FormType">
6174
<argumenttype="service"id="form.property_accessor" />
6275
<tagname="form.type"alias="form" />
@@ -69,6 +82,7 @@
6982
</service>
7083
<serviceid="form.type.choice"class="Symfony\Component\Form\Extension\Core\Type\ChoiceType">
7184
<tagname="form.type"alias="choice" />
85+
<argumenttype="service"id="form.choice_list_factory"/>
7286
</service>
7387
<serviceid="form.type.collection"class="Symfony\Component\Form\Extension\Core\Type\CollectionType">
7488
<tagname="form.type"alias="collection" />

‎src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespaceSymfony\Component\Form\Extension\Core\Type;
1313

1414
useSymfony\Component\Form\AbstractType;
15+
useSymfony\Component\Form\ChoiceList\Factory\CachingFactoryDecorator;
1516
useSymfony\Component\Form\ChoiceList\Factory\PropertyAccessDecorator;
1617
useSymfony\Component\Form\ChoiceList\LegacyChoiceListAdapter;
1718
useSymfony\Component\Form\ChoiceList\View\ChoiceGroupView;
@@ -46,7 +47,11 @@ class ChoiceType extends AbstractType
4647

4748
publicfunction__construct(ChoiceListFactoryInterface$choiceListFactory =null)
4849
{
49-
$this->choiceListFactory =$choiceListFactory ?:newPropertyAccessDecorator(newDefaultChoiceListFactory());
50+
$this->choiceListFactory =$choiceListFactory ?:newCachingFactoryDecorator(
51+
newPropertyAccessDecorator(
52+
newDefaultChoiceListFactory()
53+
)
54+
);
5055
}
5156

5257
/**

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp