Skip to content

Commit 4fb1035

Browse files
committed
fixed Doctrine EntityType when the identifier is a string
1 parent 751eaab commit 4fb1035

File tree

5 files changed

+119
-3
lines changed

5 files changed

+119
-3
lines changed

src/Symfony/Bridge/Doctrine/Form/DataTransformer/EntityToIdTransformer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function reverseTransform($key)
6565
return null;
6666
}
6767

68-
if (!is_numeric($key)) {
68+
if (count($this->choiceList->getIdentifier()) > 1 && !is_numeric($key)) {
6969
throw new UnexpectedTypeException($key, 'numeric');
7070
}
7171

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
/*
12+
namespace Symfony\Bundle\DoctrineBundle\Tests\DependencyInjection\Compiler;
13+
14+
use Symfony\Bundle\DoctrineBundle\DependencyInjection\Compiler\RegisterEventListenersAndSubscribersPass;
15+
16+
class RegisterEventListenersAndSubscribersPassTest extends TestCase
17+
{
18+
public function test()
19+
{
20+
$container->getCompilerPassConfig()->setOptimizationPasses(array());
21+
$container->getCompilerPassConfig()->setBeforeOptimizationPasses(array(new RegisterEventListenersAndSubscribersPass()));
22+
$container->getCompilerPassConfig()->setRemovingPasses(array());
23+
$container->compile();
24+
}
25+
}
26+
*/
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Symfony\Tests\Bridge\Doctrine\Form\Fixtures;
4+
5+
/** @Entity */
6+
class CompositeStringIdentEntity
7+
{
8+
/** @Id @Column(type="string") */
9+
protected $id1;
10+
11+
/** @Id @Column(type="string") */
12+
protected $id2;
13+
14+
/** @Column(type="string") */
15+
public $name;
16+
17+
public function __construct($id1, $id2, $name) {
18+
$this->id1 = $id1;
19+
$this->id2 = $id2;
20+
$this->name = $name;
21+
}
22+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Symfony\Tests\Bridge\Doctrine\Form\Fixtures;
4+
5+
/** @Entity */
6+
class SingleStringIdentEntity
7+
{
8+
/** @Id @Column(type="string") */
9+
protected $id;
10+
11+
/** @Column(type="string") */
12+
public $name;
13+
14+
public function __construct($id, $name) {
15+
$this->id = $id;
16+
$this->name = $name;
17+
}
18+
}

tests/Symfony/Tests/Bridge/Doctrine/Form/Type/EntityTypeTest.php

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,17 @@
1313

1414
require_once __DIR__.'/../DoctrineOrmTestCase.php';
1515
require_once __DIR__.'/../../Fixtures/SingleIdentEntity.php';
16+
require_once __DIR__.'/../../Fixtures/SingleStringIdentEntity.php';
1617
require_once __DIR__.'/../../Fixtures/CompositeIdentEntity.php';
18+
require_once __DIR__.'/../../Fixtures/CompositeStringIdentEntity.php';
1719

1820
use Symfony\Component\Form\Exception\UnexpectedTypeException;
1921
use Symfony\Tests\Component\Form\Extension\Core\Type\TypeTestCase;
2022
use Symfony\Tests\Bridge\Doctrine\Form\DoctrineOrmTestCase;
2123
use Symfony\Tests\Bridge\Doctrine\Form\Fixtures\SingleIdentEntity;
24+
use Symfony\Tests\Bridge\Doctrine\Form\Fixtures\SingleStringIdentEntity;
2225
use Symfony\Tests\Bridge\Doctrine\Form\Fixtures\CompositeIdentEntity;
26+
use Symfony\Tests\Bridge\Doctrine\Form\Fixtures\CompositeStringIdentEntity;
2327
use Symfony\Bridge\Doctrine\Form\DoctrineOrmExtension;
2428
use Doctrine\ORM\Tools\SchemaTool;
2529
use Doctrine\ORM\EntityManager;
@@ -28,8 +32,9 @@
2832
class EntityTypeTest extends TypeTestCase
2933
{
3034
const SINGLE_IDENT_CLASS = 'Symfony\Tests\Bridge\Doctrine\Form\Fixtures\SingleIdentEntity';
31-
35+
const SINGLE_STRING_IDENT_CLASS = 'Symfony\Tests\Bridge\Doctrine\Form\Fixtures\SingleStringIdentEntity';
3236
const COMPOSITE_IDENT_CLASS = 'Symfony\Tests\Bridge\Doctrine\Form\Fixtures\CompositeIdentEntity';
37+
const COMPOSITE_STRING_IDENT_CLASS = 'Symfony\Tests\Bridge\Doctrine\Form\Fixtures\CompositeStringIdentEntity';
3338

3439
private $em;
3540

@@ -46,7 +51,9 @@ protected function setUp()
4651
$schemaTool = new SchemaTool($this->em);
4752
$classes = array(
4853
$this->em->getClassMetadata(self::SINGLE_IDENT_CLASS),
54+
$this->em->getClassMetadata(self::SINGLE_STRING_IDENT_CLASS),
4955
$this->em->getClassMetadata(self::COMPOSITE_IDENT_CLASS),
56+
$this->em->getClassMetadata(self::COMPOSITE_STRING_IDENT_CLASS),
5057
);
5158

5259
try {
@@ -558,4 +565,47 @@ public function testDisallowChoicesThatAreNotIncludedQueryBuilderAsClosureCompos
558565
$this->assertFalse($field->isSynchronized());
559566
$this->assertNull($field->getData());
560567
}
561-
}
568+
569+
public function testSubmitSingleStringIdentifier()
570+
{
571+
$entity1 = new SingleStringIdentEntity('foo', 'Foo');
572+
573+
$this->persist(array($entity1));
574+
575+
$field = $this->factory->createNamed('entity', 'name', null, array(
576+
'multiple' => false,
577+
'expanded' => false,
578+
'em' => $this->em,
579+
'class' => self::SINGLE_STRING_IDENT_CLASS,
580+
'property' => 'name',
581+
));
582+
583+
$field->bind('foo');
584+
585+
$this->assertTrue($field->isSynchronized());
586+
$this->assertEquals($entity1, $field->getData());
587+
$this->assertEquals('foo', $field->getClientData());
588+
}
589+
590+
public function testSubmitCompositeStringIdentifier()
591+
{
592+
$entity1 = new CompositeStringIdentEntity('foo1', 'foo2', 'Foo');
593+
594+
$this->persist(array($entity1));
595+
596+
$field = $this->factory->createNamed('entity', 'name', null, array(
597+
'multiple' => false,
598+
'expanded' => false,
599+
'em' => $this->em,
600+
'class' => self::COMPOSITE_STRING_IDENT_CLASS,
601+
'property' => 'name',
602+
));
603+
604+
// the collection key is used here
605+
$field->bind('0');
606+
607+
$this->assertTrue($field->isSynchronized());
608+
$this->assertEquals($entity1, $field->getData());
609+
$this->assertEquals(0, $field->getClientData());
610+
}
611+
}

0 commit comments

Comments
 (0)