fixed Doctrine EntityType when the identifier is a string
This commit is contained in:
parent
751eaab326
commit
4fb1035578
@ -65,7 +65,7 @@ class EntityToIdTransformer implements DataTransformerInterface
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!is_numeric($key)) {
|
if (count($this->choiceList->getIdentifier()) > 1 && !is_numeric($key)) {
|
||||||
throw new UnexpectedTypeException($key, 'numeric');
|
throw new UnexpectedTypeException($key, 'numeric');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -13,13 +13,17 @@ namespace Symfony\Tests\Bridge\Doctrine\Form\Type;
|
|||||||
|
|
||||||
require_once __DIR__.'/../DoctrineOrmTestCase.php';
|
require_once __DIR__.'/../DoctrineOrmTestCase.php';
|
||||||
require_once __DIR__.'/../../Fixtures/SingleIdentEntity.php';
|
require_once __DIR__.'/../../Fixtures/SingleIdentEntity.php';
|
||||||
|
require_once __DIR__.'/../../Fixtures/SingleStringIdentEntity.php';
|
||||||
require_once __DIR__.'/../../Fixtures/CompositeIdentEntity.php';
|
require_once __DIR__.'/../../Fixtures/CompositeIdentEntity.php';
|
||||||
|
require_once __DIR__.'/../../Fixtures/CompositeStringIdentEntity.php';
|
||||||
|
|
||||||
use Symfony\Component\Form\Exception\UnexpectedTypeException;
|
use Symfony\Component\Form\Exception\UnexpectedTypeException;
|
||||||
use Symfony\Tests\Component\Form\Extension\Core\Type\TypeTestCase;
|
use Symfony\Tests\Component\Form\Extension\Core\Type\TypeTestCase;
|
||||||
use Symfony\Tests\Bridge\Doctrine\Form\DoctrineOrmTestCase;
|
use Symfony\Tests\Bridge\Doctrine\Form\DoctrineOrmTestCase;
|
||||||
use Symfony\Tests\Bridge\Doctrine\Form\Fixtures\SingleIdentEntity;
|
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\CompositeIdentEntity;
|
||||||
|
use Symfony\Tests\Bridge\Doctrine\Form\Fixtures\CompositeStringIdentEntity;
|
||||||
use Symfony\Bridge\Doctrine\Form\DoctrineOrmExtension;
|
use Symfony\Bridge\Doctrine\Form\DoctrineOrmExtension;
|
||||||
use Doctrine\ORM\Tools\SchemaTool;
|
use Doctrine\ORM\Tools\SchemaTool;
|
||||||
use Doctrine\ORM\EntityManager;
|
use Doctrine\ORM\EntityManager;
|
||||||
@ -28,8 +32,9 @@ use Doctrine\Common\Collections\ArrayCollection;
|
|||||||
class EntityTypeTest extends TypeTestCase
|
class EntityTypeTest extends TypeTestCase
|
||||||
{
|
{
|
||||||
const SINGLE_IDENT_CLASS = 'Symfony\Tests\Bridge\Doctrine\Form\Fixtures\SingleIdentEntity';
|
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_IDENT_CLASS = 'Symfony\Tests\Bridge\Doctrine\Form\Fixtures\CompositeIdentEntity';
|
||||||
|
const COMPOSITE_STRING_IDENT_CLASS = 'Symfony\Tests\Bridge\Doctrine\Form\Fixtures\CompositeStringIdentEntity';
|
||||||
|
|
||||||
private $em;
|
private $em;
|
||||||
|
|
||||||
@ -46,7 +51,9 @@ class EntityTypeTest extends TypeTestCase
|
|||||||
$schemaTool = new SchemaTool($this->em);
|
$schemaTool = new SchemaTool($this->em);
|
||||||
$classes = array(
|
$classes = array(
|
||||||
$this->em->getClassMetadata(self::SINGLE_IDENT_CLASS),
|
$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_IDENT_CLASS),
|
||||||
|
$this->em->getClassMetadata(self::COMPOSITE_STRING_IDENT_CLASS),
|
||||||
);
|
);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -558,4 +565,47 @@ class EntityTypeTest extends TypeTestCase
|
|||||||
$this->assertFalse($field->isSynchronized());
|
$this->assertFalse($field->isSynchronized());
|
||||||
$this->assertNull($field->getData());
|
$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());
|
||||||
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user