[2.0][DDC-116] Fixed.
This commit is contained in:
parent
7220cb2e8b
commit
10bc51fdcd
@ -29,6 +29,7 @@ use Doctrine\ORM\ORMException;
|
|||||||
*
|
*
|
||||||
* @since 2.0
|
* @since 2.0
|
||||||
* @author Roman Borschel <roman@code-factory.org>
|
* @author Roman Borschel <roman@code-factory.org>
|
||||||
|
* @todo Rename: AssignedGenerator?
|
||||||
*/
|
*/
|
||||||
class Assigned extends AbstractIdGenerator
|
class Assigned extends AbstractIdGenerator
|
||||||
{
|
{
|
||||||
@ -42,29 +43,27 @@ class Assigned extends AbstractIdGenerator
|
|||||||
public function generate(EntityManager $em, $entity)
|
public function generate(EntityManager $em, $entity)
|
||||||
{
|
{
|
||||||
$class = $em->getClassMetadata(get_class($entity));
|
$class = $em->getClassMetadata(get_class($entity));
|
||||||
$identifier = null;
|
$identifier = array();
|
||||||
if ($class->isIdentifierComposite()) {
|
if ($class->isIdentifierComposite) {
|
||||||
$identifier = array();
|
|
||||||
$idFields = $class->getIdentifierFieldNames();
|
$idFields = $class->getIdentifierFieldNames();
|
||||||
foreach ($idFields as $idField) {
|
foreach ($idFields as $idField) {
|
||||||
$identifier[] =
|
|
||||||
$value = $class->getReflectionProperty($idField)->getValue($entity);
|
$value = $class->getReflectionProperty($idField)->getValue($entity);
|
||||||
if (isset($value)) {
|
if (isset($value)) {
|
||||||
$identifier[] = $value;
|
$identifier[] = $value;
|
||||||
|
} else {
|
||||||
|
throw ORMException::entityMissingAssignedId($entity);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$value = $class->getReflectionProperty($class->getSingleIdentifierFieldName())
|
$value = $class->getReflectionProperty($class->getSingleIdentifierFieldName())
|
||||||
->getValue($entity);
|
->getValue($entity);
|
||||||
if (isset($value)) {
|
if (isset($value)) {
|
||||||
$identifier = array($value);
|
$identifier[] = $value;
|
||||||
|
} else {
|
||||||
|
throw ORMException::entityMissingAssignedId($entity);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( ! $identifier) {
|
|
||||||
throw ORMException::entityMissingAssignedId($entity);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $identifier;
|
return $identifier;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -20,6 +20,7 @@ class AllTests
|
|||||||
$suite = new \Doctrine\Tests\DoctrineTestSuite('Doctrine Orm Id');
|
$suite = new \Doctrine\Tests\DoctrineTestSuite('Doctrine Orm Id');
|
||||||
|
|
||||||
$suite->addTestSuite('Doctrine\Tests\ORM\Id\SequenceGeneratorTest');
|
$suite->addTestSuite('Doctrine\Tests\ORM\Id\SequenceGeneratorTest');
|
||||||
|
$suite->addTestSuite('Doctrine\Tests\ORM\Id\AssignedIdTest');
|
||||||
|
|
||||||
return $suite;
|
return $suite;
|
||||||
}
|
}
|
||||||
|
67
tests/Doctrine/Tests/ORM/Id/AssignedIdTest.php
Normal file
67
tests/Doctrine/Tests/ORM/Id/AssignedIdTest.php
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Doctrine\Tests\ORM\Id;
|
||||||
|
|
||||||
|
use Doctrine\ORM\Id\Assigned;
|
||||||
|
|
||||||
|
require_once __DIR__ . '/../../TestInit.php';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AssignedIdTest
|
||||||
|
*
|
||||||
|
* @author robo
|
||||||
|
*/
|
||||||
|
class AssignedIdTest extends \Doctrine\Tests\OrmTestCase
|
||||||
|
{
|
||||||
|
private $_em;
|
||||||
|
private $_assignedGen;
|
||||||
|
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
$this->_em = $this->_getTestEntityManager();
|
||||||
|
$this->_assignedGen = new Assigned;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testThrowsExceptionIfIdNotAssigned()
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$entity = new AssignedSingleIdEntity;
|
||||||
|
$this->_assignedGen->generate($this->_em, $entity);
|
||||||
|
$this->fail('Assigned generator did not throw exception even though ID was missing.');
|
||||||
|
} catch (\Doctrine\ORM\ORMException $expected) {}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$entity = new AssignedCompositeIdEntity;
|
||||||
|
$this->_assignedGen->generate($this->_em, $entity);
|
||||||
|
$this->fail('Assigned generator did not throw exception even though ID was missing.');
|
||||||
|
} catch (\Doctrine\ORM\ORMException $expected) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCorrectIdGeneration()
|
||||||
|
{
|
||||||
|
$entity = new AssignedSingleIdEntity;
|
||||||
|
$entity->myId = 1;
|
||||||
|
$id = $this->_assignedGen->generate($this->_em, $entity);
|
||||||
|
$this->assertEquals(array(1), $id);
|
||||||
|
|
||||||
|
$entity = new AssignedCompositeIdEntity;
|
||||||
|
$entity->myId2 = 2;
|
||||||
|
$entity->myId1 = 4;
|
||||||
|
$id = $this->_assignedGen->generate($this->_em, $entity);
|
||||||
|
$this->assertEquals(array(4, 2), $id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @Entity */
|
||||||
|
class AssignedSingleIdEntity {
|
||||||
|
/** @Id @Column(type="integer") */
|
||||||
|
public $myId;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @Entity */
|
||||||
|
class AssignedCompositeIdEntity {
|
||||||
|
/** @Id @Column(type="integer") */
|
||||||
|
public $myId1;
|
||||||
|
/** @Id @Column(type="integer") */
|
||||||
|
public $myId2;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user