From a91050e7f4eb40b35fd8f90b16359c754e05a8ee Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Tue, 25 Jun 2013 19:34:12 +0200 Subject: [PATCH] [DDC-2350] Eager Collections are not marked as initialized, leading to multiple queries being executed. --- lib/Doctrine/ORM/UnitOfWork.php | 2 + .../ORM/Functional/Ticket/DDC2350Test.php | 68 +++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 tests/Doctrine/Tests/ORM/Functional/Ticket/DDC2350Test.php diff --git a/lib/Doctrine/ORM/UnitOfWork.php b/lib/Doctrine/ORM/UnitOfWork.php index 68fde5d2c..62bce139d 100644 --- a/lib/Doctrine/ORM/UnitOfWork.php +++ b/lib/Doctrine/ORM/UnitOfWork.php @@ -2773,6 +2773,8 @@ class UnitOfWork implements PropertyChangedListener $persister->loadManyToManyCollection($assoc, $collection->getOwner(), $collection); break; } + + $collection->setInitialized(true); } /** diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC2350Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC2350Test.php new file mode 100644 index 000000000..229bbff52 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC2350Test.php @@ -0,0 +1,68 @@ +_schemaTool->createSchema(array( + $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC2350User'), + $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC2350Bug'), + )); + } + + public function testEagerCollectionsAreOnlyRetrievedOnce() + { + $user = new DDC2350User(); + $bug1 = new DDC2350Bug(); + $bug1->user = $user; + $bug2 = new DDC2350Bug(); + $bug2->user = $user; + + $this->_em->persist($user); + $this->_em->persist($bug1); + $this->_em->persist($bug2); + $this->_em->flush(); + + $this->_em->clear(); + + $cnt = $this->getCurrentQueryCount(); + $user = $this->_em->find(__NAMESPACE__ . '\DDC2350User', $user->id); + + $this->assertEquals($cnt + 2, $this->getCurrentQueryCount()); + + $this->assertEquals(2, count($user->reportedBugs)); + + $this->assertEquals($cnt + 2, $this->getCurrentQueryCount()); + } +} + +/** + * @Entity + */ +class DDC2350User +{ + /** @Id @Column(type="integer") @GeneratedValue */ + public $id; + /** @OneToMany(targetEntity="DDC2350Bug", mappedBy="user", fetch="EAGER") */ + public $reportedBugs; +} + +/** + * @Entity + */ +class DDC2350Bug +{ + /** @Id @Column(type="integer") @GeneratedValue */ + public $id; + /** @ManyToOne(targetEntity="DDC2350User", inversedBy="reportedBugs") */ + public $user; +}