Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add missing EntityToIDTransformer files
- Loading branch information
Showing
2 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
src/Symfony/Bundle/DoctrineBundle/Form/ValueTransformer/EntityToIDTransformer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
namespace Symfony\Bundle\DoctrineBundle\Form\ValueTransformer; | ||
|
||
use Symfony\Component\Form\ValueTransformer\BaseValueTransformer; | ||
use Symfony\Component\Form\ValueTransformer\TransformationFailedException; | ||
|
||
/* | ||
* This file is part of the Symfony framework. | ||
* | ||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
/** | ||
* Transforms a Doctrine Entity into its identifier value and back. | ||
* | ||
* This only works with single-field primary key fields. | ||
* | ||
* @author Benjamin Eberlei <kontakt@beberlei.de> | ||
*/ | ||
class EntityToIDTransformer extends BaseValueTransformer | ||
{ | ||
protected function configure() | ||
{ | ||
$this->addRequiredOption('em'); | ||
$this->addRequiredOption('className'); | ||
} | ||
|
||
/** | ||
* Reverse Transforming the selected id value to an Doctrine Entity. | ||
* | ||
* This handles NULL, the EntityManager#find method returns null if no entity was found. | ||
* | ||
* @param int|string $newId | ||
* @param object $oldEntity | ||
* @return object | ||
*/ | ||
public function reverseTransform($newId, $oldEntity) | ||
{ | ||
if (empty($newId)) { | ||
return null; | ||
} | ||
|
||
return $this->getOption('em')->find($this->getOption('className'), $newId); | ||
} | ||
|
||
/** | ||
* @param object $entity | ||
* @return int|string | ||
*/ | ||
public function transform($entity) | ||
{ | ||
if (empty($entity)) { | ||
return 0; | ||
} | ||
|
||
return current( $this->getOption('em')->getUnitOfWork()->getEntityIdentifier($entity) ); | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
src/Symfony/Bundle/DoctrineBundle/Tests/Form/ValueTransformer/EntityToIDTransformerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
|
||
namespace Symfony\Bundle\DoctrineBundle\Tests\Form\ValueTransformer; | ||
|
||
use Doctrine\Common\Collections\ArrayCollection; | ||
use Doctrine\ORM\Tools\SchemaTool; | ||
use Symfony\Bundle\DoctrineBundle\Form\ValueTransformer\EntityToIDTransformer; | ||
|
||
class EntityToIDTransformerTest extends \Symfony\Bundle\DoctrineBundle\Tests\TestCase | ||
{ | ||
/** | ||
* @var EntityManager | ||
*/ | ||
private $em; | ||
|
||
public function setUp() | ||
{ | ||
parent::setUp(); | ||
$this->em = $this->createTestEntityManager(); | ||
|
||
$schemaTool = new SchemaTool($this->em); | ||
$classes = array($this->em->getClassMetadata('Symfony\Bundle\DoctrineBundle\Tests\Form\ValueTransformer\Tag')); | ||
try { | ||
$schemaTool->dropSchema($classes); | ||
} catch(\Exception $e) { | ||
|
||
} | ||
try { | ||
$schemaTool->createSchema($classes); | ||
} catch(\Exception $e) { | ||
|
||
} | ||
} | ||
|
||
public function testRequiredEntityManager() | ||
{ | ||
$this->setExpectedException('Symfony\Component\Form\Exception\MissingOptionsException'); | ||
$transformer = new EntityToIDTransformer(array('className' => 'Symfony\Bundle\DoctrineBundle\Tests\Form\ValueTransformer\Tag')); | ||
} | ||
|
||
public function testRequiredClassName() | ||
{ | ||
$this->setExpectedException('Symfony\Component\Form\Exception\MissingOptionsException'); | ||
$transformer = new EntityToIDTransformer(array('em' => $this->em)); | ||
} | ||
|
||
public function createTransformer() | ||
{ | ||
$transformer = new EntityToIDTransformer(array( | ||
'em' => $this->em, | ||
'className' => 'Symfony\Bundle\DoctrineBundle\Tests\Form\ValueTransformer\Tag' | ||
)); | ||
return $transformer; | ||
} | ||
|
||
public function testTranformEmptyValueReturnsNull() | ||
{ | ||
$transformer = $this->createTransformer(); | ||
$this->assertEquals(0, $transformer->transform(null)); | ||
$this->assertEquals(0, $transformer->transform("")); | ||
$this->assertEquals(0, $transformer->transform(0)); | ||
} | ||
|
||
public function testTransform() | ||
{ | ||
$transformer = $this->createTransformer(); | ||
|
||
$tag = new Tag("name"); | ||
$this->em->persist($tag); | ||
$this->em->flush(); | ||
|
||
$this->assertEquals(1, $transformer->transform($tag)); | ||
} | ||
|
||
public function testReverseTransformEmptyValue() | ||
{ | ||
$transformer = $this->createTransformer(); | ||
$this->assertNull($transformer->reverseTransform(0, null)); | ||
} | ||
|
||
public function testReverseTransform() | ||
{ | ||
$transformer = $this->createTransformer(); | ||
|
||
$tag = new Tag("name"); | ||
$this->em->persist($tag); | ||
$this->em->flush(); | ||
|
||
$this->assertSame($tag, $transformer->reverseTransform(1, null)); | ||
} | ||
} |