From c648981f28bd6279ba6dbcfe9ae3adf61dfc6f00 Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Sun, 13 Nov 2011 23:14:31 +0100 Subject: [PATCH] DDC-1461 - Verified deferred explicit works --- .../Doctrine/Tests/Models/CMS/CmsAddress.php | 6 +- .../ORM/Functional/Ticket/DDC1461Test.php | 112 ++++++++++++++++++ 2 files changed, 115 insertions(+), 3 deletions(-) create mode 100644 tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1461Test.php diff --git a/tests/Doctrine/Tests/Models/CMS/CmsAddress.php b/tests/Doctrine/Tests/Models/CMS/CmsAddress.php index 9db2b6475..9119a6f58 100644 --- a/tests/Doctrine/Tests/Models/CMS/CmsAddress.php +++ b/tests/Doctrine/Tests/Models/CMS/CmsAddress.php @@ -38,7 +38,7 @@ class CmsAddress public $street; /** - * @OneToOne(targetEntity="CmsUser", inversedBy="address") + * @OneToOne(targetEntity="CmsUser", inversedBy="address", cascade={"persist"}) * @JoinColumn(referencedColumnName="id") */ public $user; @@ -46,7 +46,7 @@ class CmsAddress public function getId() { return $this->id; } - + public function getUser() { return $this->user; } @@ -62,7 +62,7 @@ class CmsAddress public function getCity() { return $this->city; } - + public function setUser(CmsUser $user) { if ($this->user !== $user) { $this->user = $user; diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1461Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1461Test.php new file mode 100644 index 000000000..2f59ede23 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1461Test.php @@ -0,0 +1,112 @@ +useModelSet('cms'); + parent::setUp(); + + try { + $this->_schemaTool->createSchema(array( + $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC1461TwitterAccount'), + $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC1461User') + )); + } catch(\Exception $e) { + + } + } + + public function testChangeDetectionDeferredImplitic() + { + $address = new \Doctrine\Tests\Models\CMS\CmsAddress(); + $address->city = "Karlsruhe"; + $address->country = "Germany"; + $address->street = "somestreet"; + $address->zip = 12345; + + $this->_em->persist($address); + $this->_em->flush(); + + $user = new CmsUser(); + $user->name = "schmittjoh"; + $user->username = "schmittjoh"; + $user->status = "active"; + + $address->setUser($user); + $this->_em->flush(); + $this->_em->clear(); + + $user = $this->_em->find(get_class($user), $user->getId()); + $this->assertNotNull($user->getAddress()); + $this->assertEquals("Karlsruhe", $user->getAddress()->getCity()); + } + + public function testChangeDetectionDeferredExplicit() + { + $user = new DDC1461User; + $this->_em->persist($user); + $this->_em->flush(); + + $this->assertEquals(\Doctrine\ORM\UnitOfWork::STATE_MANAGED, $this->_em->getUnitOfWork()->getEntityState($user, \Doctrine\ORM\UnitOfWork::STATE_NEW), "Entity should be managed."); + $this->assertEquals(\Doctrine\ORM\UnitOfWork::STATE_MANAGED, $this->_em->getUnitOfWork()->getEntityState($user), "Entity should be managed."); + + $acc = new DDC1461TwitterAccount; + $user->twitterAccount = $acc; + + $this->_em->persist($user); + $this->_em->flush(); + + $user = $this->_em->find(get_class($user), $user->id); + $this->assertNotNull($user->twitterAccount); + } +} + +/** + * @Entity + * @ChangeTrackingPolicy("DEFERRED_EXPLICIT") + */ +class DDC1461User +{ + /** + * @Id + * @GeneratedValue(strategy="AUTO") + * @Column(type="integer") + */ + public $id; + + /** + * @OneToOne(targetEntity="DDC1461TwitterAccount", orphanRemoval=true, fetch="EAGER", cascade = {"persist"}, inversedBy="user") + * @var TwitterAccount + */ + public $twitterAccount; +} + +/** + * @Entity + * @ChangeTrackingPolicy("DEFERRED_EXPLICIT") + */ +class DDC1461TwitterAccount +{ + /** + * @Id + * @GeneratedValue(strategy="AUTO") + * @Column(type="integer") + */ + public $id; + + /** + * @OneToOne(targetEntity="DDC1461User", fetch="EAGER") + */ + public $user; +} \ No newline at end of file