Commit 10bc51fd authored by romanb's avatar romanb

[2.0][DDC-116] Fixed.

parent 7220cb2e
......@@ -29,6 +29,7 @@ use Doctrine\ORM\ORMException;
*
* @since 2.0
* @author Roman Borschel <roman@code-factory.org>
* @todo Rename: AssignedGenerator?
*/
class Assigned extends AbstractIdGenerator
{
......@@ -42,28 +43,26 @@ class Assigned extends AbstractIdGenerator
public function generate(EntityManager $em, $entity)
{
$class = $em->getClassMetadata(get_class($entity));
$identifier = null;
if ($class->isIdentifierComposite()) {
$identifier = array();
$identifier = array();
if ($class->isIdentifierComposite) {
$idFields = $class->getIdentifierFieldNames();
foreach ($idFields as $idField) {
$identifier[] =
$value = $class->getReflectionProperty($idField)->getValue($entity);
if (isset($value)) {
$identifier[] = $value;
} else {
throw ORMException::entityMissingAssignedId($entity);
}
}
} else {
$value = $class->getReflectionProperty($class->getSingleIdentifierFieldName())
->getValue($entity);
if (isset($value)) {
$identifier = array($value);
$identifier[] = $value;
} else {
throw ORMException::entityMissingAssignedId($entity);
}
}
if ( ! $identifier) {
throw ORMException::entityMissingAssignedId($entity);
}
return $identifier;
}
......
......@@ -20,6 +20,7 @@ class AllTests
$suite = new \Doctrine\Tests\DoctrineTestSuite('Doctrine Orm Id');
$suite->addTestSuite('Doctrine\Tests\ORM\Id\SequenceGeneratorTest');
$suite->addTestSuite('Doctrine\Tests\ORM\Id\AssignedIdTest');
return $suite;
}
......
<?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;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment