瀏覽代碼

fixed Doctrine EntityType when the identifier is a string

Fabien Potencier 14 年之前
父節點
當前提交
4fb1035578

+ 1 - 1
src/Symfony/Bridge/Doctrine/Form/DataTransformer/EntityToIdTransformer.php

@@ -65,7 +65,7 @@ class EntityToIdTransformer implements DataTransformerInterface
             return null;
         }
 
-        if (!is_numeric($key)) {
+        if (count($this->choiceList->getIdentifier()) > 1 && !is_numeric($key)) {
             throw new UnexpectedTypeException($key, 'numeric');
         }
 

+ 26 - 0
src/Symfony/Bundle/DoctrineBundle/Tests/DependencyInjection/Compiler/RegisterEventListenersAndSubscribersPassTest.php

@@ -0,0 +1,26 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+/*
+namespace Symfony\Bundle\DoctrineBundle\Tests\DependencyInjection\Compiler;
+
+use Symfony\Bundle\DoctrineBundle\DependencyInjection\Compiler\RegisterEventListenersAndSubscribersPass;
+
+class RegisterEventListenersAndSubscribersPassTest extends TestCase
+{
+    public function test()
+    {
+        $container->getCompilerPassConfig()->setOptimizationPasses(array());
+        $container->getCompilerPassConfig()->setBeforeOptimizationPasses(array(new RegisterEventListenersAndSubscribersPass()));
+        $container->getCompilerPassConfig()->setRemovingPasses(array());
+        $container->compile();
+    }
+}
+*/

+ 22 - 0
tests/Symfony/Tests/Bridge/Doctrine/Fixtures/CompositeStringIdentEntity.php

@@ -0,0 +1,22 @@
+<?php
+
+namespace Symfony\Tests\Bridge\Doctrine\Form\Fixtures;
+
+/** @Entity */
+class CompositeStringIdentEntity
+{
+    /** @Id @Column(type="string") */
+    protected $id1;
+
+    /** @Id @Column(type="string") */
+    protected $id2;
+
+    /** @Column(type="string") */
+    public $name;
+
+    public function __construct($id1, $id2, $name) {
+        $this->id1 = $id1;
+        $this->id2 = $id2;
+        $this->name = $name;
+    }
+}

+ 18 - 0
tests/Symfony/Tests/Bridge/Doctrine/Fixtures/SingleStringIdentEntity.php

@@ -0,0 +1,18 @@
+<?php
+
+namespace Symfony\Tests\Bridge\Doctrine\Form\Fixtures;
+
+/** @Entity */
+class SingleStringIdentEntity
+{
+    /** @Id @Column(type="string") */
+    protected $id;
+
+    /** @Column(type="string") */
+    public $name;
+
+    public function __construct($id, $name) {
+        $this->id = $id;
+        $this->name = $name;
+    }
+}

+ 52 - 2
tests/Symfony/Tests/Bridge/Doctrine/Form/Type/EntityTypeTest.php

@@ -13,13 +13,17 @@ namespace Symfony\Tests\Bridge\Doctrine\Form\Type;
 
 require_once __DIR__.'/../DoctrineOrmTestCase.php';
 require_once __DIR__.'/../../Fixtures/SingleIdentEntity.php';
+require_once __DIR__.'/../../Fixtures/SingleStringIdentEntity.php';
 require_once __DIR__.'/../../Fixtures/CompositeIdentEntity.php';
+require_once __DIR__.'/../../Fixtures/CompositeStringIdentEntity.php';
 
 use Symfony\Component\Form\Exception\UnexpectedTypeException;
 use Symfony\Tests\Component\Form\Extension\Core\Type\TypeTestCase;
 use Symfony\Tests\Bridge\Doctrine\Form\DoctrineOrmTestCase;
 use Symfony\Tests\Bridge\Doctrine\Form\Fixtures\SingleIdentEntity;
+use Symfony\Tests\Bridge\Doctrine\Form\Fixtures\SingleStringIdentEntity;
 use Symfony\Tests\Bridge\Doctrine\Form\Fixtures\CompositeIdentEntity;
+use Symfony\Tests\Bridge\Doctrine\Form\Fixtures\CompositeStringIdentEntity;
 use Symfony\Bridge\Doctrine\Form\DoctrineOrmExtension;
 use Doctrine\ORM\Tools\SchemaTool;
 use Doctrine\ORM\EntityManager;
@@ -28,8 +32,9 @@ use Doctrine\Common\Collections\ArrayCollection;
 class EntityTypeTest extends TypeTestCase
 {
     const SINGLE_IDENT_CLASS = 'Symfony\Tests\Bridge\Doctrine\Form\Fixtures\SingleIdentEntity';
-
+    const SINGLE_STRING_IDENT_CLASS = 'Symfony\Tests\Bridge\Doctrine\Form\Fixtures\SingleStringIdentEntity';
     const COMPOSITE_IDENT_CLASS = 'Symfony\Tests\Bridge\Doctrine\Form\Fixtures\CompositeIdentEntity';
+    const COMPOSITE_STRING_IDENT_CLASS = 'Symfony\Tests\Bridge\Doctrine\Form\Fixtures\CompositeStringIdentEntity';
 
     private $em;
 
@@ -46,7 +51,9 @@ class EntityTypeTest extends TypeTestCase
         $schemaTool = new SchemaTool($this->em);
         $classes = array(
             $this->em->getClassMetadata(self::SINGLE_IDENT_CLASS),
+            $this->em->getClassMetadata(self::SINGLE_STRING_IDENT_CLASS),
             $this->em->getClassMetadata(self::COMPOSITE_IDENT_CLASS),
+            $this->em->getClassMetadata(self::COMPOSITE_STRING_IDENT_CLASS),
         );
 
         try {
@@ -558,4 +565,47 @@ class EntityTypeTest extends TypeTestCase
         $this->assertFalse($field->isSynchronized());
         $this->assertNull($field->getData());
     }
-}
+
+    public function testSubmitSingleStringIdentifier()
+    {
+        $entity1 = new SingleStringIdentEntity('foo', 'Foo');
+
+        $this->persist(array($entity1));
+
+        $field = $this->factory->createNamed('entity', 'name', null, array(
+            'multiple' => false,
+            'expanded' => false,
+            'em' => $this->em,
+            'class' => self::SINGLE_STRING_IDENT_CLASS,
+            'property' => 'name',
+        ));
+
+        $field->bind('foo');
+
+        $this->assertTrue($field->isSynchronized());
+        $this->assertEquals($entity1, $field->getData());
+        $this->assertEquals('foo', $field->getClientData());
+    }
+
+    public function testSubmitCompositeStringIdentifier()
+    {
+        $entity1 = new CompositeStringIdentEntity('foo1', 'foo2', 'Foo');
+
+        $this->persist(array($entity1));
+
+        $field = $this->factory->createNamed('entity', 'name', null, array(
+            'multiple' => false,
+            'expanded' => false,
+            'em' => $this->em,
+            'class' => self::COMPOSITE_STRING_IDENT_CLASS,
+            'property' => 'name',
+        ));
+
+        // the collection key is used here
+        $field->bind('0');
+
+        $this->assertTrue($field->isSynchronized());
+        $this->assertEquals($entity1, $field->getData());
+        $this->assertEquals(0, $field->getClientData());
+    }
+}