From da2d83fc7d8170cdaffcb70373f20ddd8a1bc7b0 Mon Sep 17 00:00:00 2001 From: Michael Ridgway Date: Wed, 15 Jun 2011 17:15:46 -0400 Subject: [PATCH 1/2] DDC-1209 tests --- lib/Doctrine/ORM/ORMException.php | 2 +- .../ORM/Functional/Ticket/DDC1209Test.php | 125 ++++++++++++++++++ 2 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1209Test.php diff --git a/lib/Doctrine/ORM/ORMException.php b/lib/Doctrine/ORM/ORMException.php index 0825ae87d99..75b91d2e4d8 100644 --- a/lib/Doctrine/ORM/ORMException.php +++ b/lib/Doctrine/ORM/ORMException.php @@ -38,7 +38,7 @@ public static function missingMappingDriverImpl() public static function entityMissingForeignAssignedId($entity, $relatedEntity) { return new self( - "Entity of type " . get_class($entity) . " has identity through a foreign entity " . get_class($relatedEntityClass) . ", " . + "Entity of type " . get_class($entity) . " has identity through a foreign entity " . get_class($relatedEntity) . ", " . "however this entity has no ientity itself. You have to call EntityManager#persist() on the related entity " . "and make sure it an identifier was generated before trying to persist '" . get_class($entity) . "'. In case " . "of Post Insert ID Generation (such as MySQL Auto-Increment or PostgreSQL SERIAL) this means you have to call " . diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1209Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1209Test.php new file mode 100644 index 00000000000..8ff5e52a5e8 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1209Test.php @@ -0,0 +1,125 @@ +_schemaTool->createSchema(array( + $this->_em->getClassMetadata(__NAMESPACE__ . '\\DDC1209_1'), + $this->_em->getClassMetadata(__NAMESPACE__ . '\\DDC1209_2'), + $this->_em->getClassMetadata(__NAMESPACE__ . '\\DDC1209_3') + )); + } catch(\Exception $e) { + } + } + + /** + * @group DDC-1209 + */ + public function testIdentifierCanHaveCustomType() + { + $this->_em->persist(new DDC1209_3()); + $this->_em->flush(); + } + + /** + * @group DDC-1209 + */ + public function testCompositeIdentifierCanHaveCustomType() + { + $future1 = new DDC1209_1(); + $this->_em->persist($future1); + + $this->_em->flush(); + + $future2 = new DDC1209_2($future1); + $this->_em->persist($future2); + + $this->_em->flush(); + } +} + +/** + * @Entity + */ +class DDC1209_1 +{ + /** + * @Id @GeneratedValue @Column(type="integer") + */ + private $id; + + public function getId() + { + return $this->id; + } +} + +/** + * @Entity + */ +class DDC1209_2 +{ + /** + * @Id + * @ManyToOne(targetEntity="DDC1209_1") + * @JoinColumn(referencedColumnName="id", nullable=false) + */ + private $future1; + /** + * @Id + * @Column(type="datetime", nullable=false) + */ + private $starting_datetime; + /** + * @Id + * @Column(type="datetime", nullable=false) + */ + private $during_datetime; + /** + * @Id + * @Column(type="datetime", nullable=false) + */ + private $ending_datetime; + + public function __construct(DDC1209_1 $future1) + { + $this->future1 = $future1; + $this->starting_datetime = new \DateTime(); + $this->during_datetime = new \DateTime(); + $this->ending_datetime = new \DateTime(); + } +} + +/** + * @Entity + */ +class DDC1209_3 +{ + /** + * @Id + * @Column(type="datetime") + */ + private $date; + + public function __construct() + { + $this->date = new DateTime2(); + } +} + +class DateTime2 extends \DateTime +{ + public function __toString() + { + return $this->form('Y'); + } +} \ No newline at end of file From d1106a730bf541c2e8fa819e8ac1c2431c342fd1 Mon Sep 17 00:00:00 2001 From: Michael Ridgway Date: Thu, 16 Jun 2011 08:55:09 -0400 Subject: [PATCH 2/2] Made DDC-1209 test pass --- lib/Doctrine/ORM/Id/AssignedGenerator.php | 4 ++-- .../Doctrine/Tests/ORM/Functional/Ticket/DDC1209Test.php | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/Doctrine/ORM/Id/AssignedGenerator.php b/lib/Doctrine/ORM/Id/AssignedGenerator.php index 63c6e5418bc..05c3790afbb 100644 --- a/lib/Doctrine/ORM/Id/AssignedGenerator.php +++ b/lib/Doctrine/ORM/Id/AssignedGenerator.php @@ -49,7 +49,7 @@ public function generate(EntityManager $em, $entity) foreach ($idFields as $idField) { $value = $class->reflFields[$idField]->getValue($entity); if (isset($value)) { - if (is_object($value)) { + if (isset($class->associationMappings[$idField])) { if (!$em->getUnitOfWork()->isInIdentityMap($value)) { throw ORMException::entityMissingForeignAssignedId($entity, $value); } @@ -67,7 +67,7 @@ public function generate(EntityManager $em, $entity) $idField = $class->identifier[0]; $value = $class->reflFields[$idField]->getValue($entity); if (isset($value)) { - if (is_object($value)) { + if (isset($class->associationMappings[$idField])) { if (!$em->getUnitOfWork()->isInIdentityMap($value)) { throw ORMException::entityMissingForeignAssignedId($entity, $value); } diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1209Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1209Test.php index 8ff5e52a5e8..472978bc261 100644 --- a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1209Test.php +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1209Test.php @@ -93,9 +93,9 @@ class DDC1209_2 public function __construct(DDC1209_1 $future1) { $this->future1 = $future1; - $this->starting_datetime = new \DateTime(); - $this->during_datetime = new \DateTime(); - $this->ending_datetime = new \DateTime(); + $this->starting_datetime = new DateTime2(); + $this->during_datetime = new DateTime2(); + $this->ending_datetime = new DateTime2(); } } @@ -120,6 +120,6 @@ class DateTime2 extends \DateTime { public function __toString() { - return $this->form('Y'); + return $this->format('Y'); } } \ No newline at end of file