From ca5d9acb19459c605c66c60e6b79e8ef5e57bbd9 Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Thu, 6 Dec 2012 19:23:55 +0100 Subject: [PATCH] [DoctrineBridge] Fixed caching in DoctrineType when "choices" or "preferred_choices" is passed --- .../Doctrine/Form/Type/DoctrineType.php | 8 ++-- .../Form/Type/EntityTypePerformanceTest.php | 45 ++++++++++++++++++- 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Bridge/Doctrine/Form/Type/DoctrineType.php b/src/Symfony/Bridge/Doctrine/Form/Type/DoctrineType.php index 5ac205a1d7..0cdfe03cd0 100644 --- a/src/Symfony/Bridge/Doctrine/Form/Type/DoctrineType.php +++ b/src/Symfony/Bridge/Doctrine/Form/Type/DoctrineType.php @@ -77,16 +77,16 @@ abstract class DoctrineType extends AbstractType // A second parameter ($key) is passed, so we cannot use // spl_object_hash() directly (which strictly requires // one parameter) - array_walk_recursive($choiceHashes, function ($value) { - return spl_object_hash($value); + array_walk_recursive($choiceHashes, function (&$value) { + $value = spl_object_hash($value); }); } $preferredChoiceHashes = $options['preferred_choices']; if (is_array($preferredChoiceHashes)) { - array_walk_recursive($preferredChoiceHashes, function ($value) { - return spl_object_hash($value); + array_walk_recursive($preferredChoiceHashes, function (&$value) { + $value = spl_object_hash($value); }); } diff --git a/src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypePerformanceTest.php b/src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypePerformanceTest.php index e73dd11be0..8c3ee842d5 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypePerformanceTest.php +++ b/src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypePerformanceTest.php @@ -104,7 +104,7 @@ class EntityTypePerformanceTest extends FormPerformanceTestCase { $this->setMaxRunningTime(1); - for ($i = 0; $i < 20; ++$i) { + for ($i = 0; $i < 40; ++$i) { $form = $this->factory->create('entity', null, array( 'class' => self::ENTITY_CLASS, )); @@ -114,11 +114,14 @@ class EntityTypePerformanceTest extends FormPerformanceTestCase } } + /** + * @group benchmark + */ public function testCollapsedEntityFieldWithQueryBuilder() { $this->setMaxRunningTime(1); - for ($i = 0; $i < 20; ++$i) { + for ($i = 0; $i < 40; ++$i) { $form = $this->factory->create('entity', null, array( 'class' => self::ENTITY_CLASS, 'query_builder' => function (EntityRepository $repo) { @@ -130,4 +133,42 @@ class EntityTypePerformanceTest extends FormPerformanceTestCase $form->createView(); } } + + /** + * @group benchmark + */ + public function testCollapsedEntityFieldWithChoices() + { + $choices = $this->em->createQuery('SELECT c FROM ' . self::ENTITY_CLASS . ' c')->getResult(); + $this->setMaxRunningTime(1); + + for ($i = 0; $i < 40; ++$i) { + $form = $this->factory->create('entity', null, array( + 'class' => self::ENTITY_CLASS, + 'choices' => $choices, + )); + + // force loading of the choice list + $form->createView(); + } + } + + /** + * @group benchmark + */ + public function testCollapsedEntityFieldWithPreferredChoices() + { + $choices = $this->em->createQuery('SELECT c FROM ' . self::ENTITY_CLASS . ' c')->getResult(); + $this->setMaxRunningTime(1); + + for ($i = 0; $i < 40; ++$i) { + $form = $this->factory->create('entity', null, array( + 'class' => self::ENTITY_CLASS, + 'preferred_choices' => $choices, + )); + + // force loading of the choice list + $form->createView(); + } + } }