diff --git a/lib/Doctrine/ORM/Persisters/OneToManyPersister.php b/lib/Doctrine/ORM/Persisters/OneToManyPersister.php index 5e889ddb9..e9fcf06c5 100644 --- a/lib/Doctrine/ORM/Persisters/OneToManyPersister.php +++ b/lib/Doctrine/ORM/Persisters/OneToManyPersister.php @@ -124,24 +124,26 @@ class OneToManyPersister extends AbstractCollectionPersister public function count(PersistentCollection $coll) { $mapping = $coll->getMapping(); - $class = $this->_em->getClassMetadata($mapping['targetEntity']); + $targetClass = $this->_em->getClassMetadata($mapping['targetEntity']); + $sourceClass = $this->_em->getClassMetadata($mapping['sourceEntity']); + $params = array(); $id = $this->_em->getUnitOfWork()->getEntityIdentifier($coll->getOwner()); $where = ''; - foreach ($class->associationMappings[$mapping['mappedBy']]['joinColumns'] AS $joinColumn) { + foreach ($targetClass->associationMappings[$mapping['mappedBy']]['joinColumns'] AS $joinColumn) { if ($where != '') { $where .= ' AND '; } $where .= $joinColumn['name'] . " = ?"; - if ($class->containsForeignIdentifier) { - $params[] = $id[$class->getFieldForColumn($joinColumn['referencedColumnName'])]; + if ($targetClass->containsForeignIdentifier) { + $params[] = $id[$sourceClass->getFieldForColumn($joinColumn['referencedColumnName'])]; } else { - $params[] = $id[$class->fieldNames[$joinColumn['referencedColumnName']]]; + $params[] = $id[$sourceClass->fieldNames[$joinColumn['referencedColumnName']]]; } } - $sql = "SELECT count(*) FROM " . $class->getQuotedTableName($this->_conn->getDatabasePlatform()) . " WHERE " . $where; + $sql = "SELECT count(*) FROM " . $targetClass->getQuotedTableName($this->_conn->getDatabasePlatform()) . " WHERE " . $where; return $this->_conn->fetchColumn($sql, $params); } @@ -180,4 +182,4 @@ class OneToManyPersister extends AbstractCollectionPersister return $uow->getEntityPersister($mapping['targetEntity']) ->exists($element, array($mapping['mappedBy'] => $id)); } -} \ No newline at end of file +} diff --git a/tests/Doctrine/Tests/Models/Legacy/LegacyArticle.php b/tests/Doctrine/Tests/Models/Legacy/LegacyArticle.php new file mode 100644 index 000000000..fb754462e --- /dev/null +++ b/tests/Doctrine/Tests/Models/Legacy/LegacyArticle.php @@ -0,0 +1,33 @@ +_user = $author; + } +} diff --git a/tests/Doctrine/Tests/Models/Legacy/LegacyCar.php b/tests/Doctrine/Tests/Models/Legacy/LegacyCar.php new file mode 100644 index 000000000..ac3834145 --- /dev/null +++ b/tests/Doctrine/Tests/Models/Legacy/LegacyCar.php @@ -0,0 +1,41 @@ +_description; + } + + public function addUser(LegacyUser $user) { + $this->_users[] = $user; + } + + public function getUsers() { + return $this->_users; + } +} diff --git a/tests/Doctrine/Tests/Models/Legacy/LegacyUser.php b/tests/Doctrine/Tests/Models/Legacy/LegacyUser.php new file mode 100644 index 000000000..d7c9a5c37 --- /dev/null +++ b/tests/Doctrine/Tests/Models/Legacy/LegacyUser.php @@ -0,0 +1,80 @@ +_articles = new ArrayCollection; + $this->_references = new ArrayCollection; + $this->_cars = new ArrayCollection; + } + + public function getId() { + return $this->_id; + } + + public function getUsername() { + return $this->_username; + } + + public function addArticle(LegacyArticle $article) { + $this->_articles[] = $article; + $article->setAuthor($this); + } + + public function addReference($reference) + { + $this->_references[] = $reference; + } + + public function references() + { + return $this->_references; + } + + public function addCar(LegacyCar $car) { + $this->_cars[] = $car; + $car->addUser($this); + } + + public function getCars() { + return $this->_cars; + } +} diff --git a/tests/Doctrine/Tests/Models/Legacy/LegacyUserReference.php b/tests/Doctrine/Tests/Models/Legacy/LegacyUserReference.php new file mode 100644 index 000000000..c6cd891a1 --- /dev/null +++ b/tests/Doctrine/Tests/Models/Legacy/LegacyUserReference.php @@ -0,0 +1,65 @@ +addReference($this); + $target->addReference($this); + + $this->_source = $source; + $this->_target = $target; + $this->_description = $description; + $this->_created = new \DateTime("now"); + } + + public function source() + { + return $this->_source; + } + + public function target() + { + return $this->_target; + } + + public function setDescription($desc) + { + $this->_description = $desc; + } + + public function getDescription() + { + return $this->_description; + } +} diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1301Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1301Test.php new file mode 100644 index 000000000..94d02f905 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1301Test.php @@ -0,0 +1,148 @@ +useModelSet('legacy'); + parent::setUp(); + + $class = $this->_em->getClassMetadata('Doctrine\Tests\Models\Legacy\LegacyUser'); + $class->associationMappings['_articles']['fetch'] = ClassMetadataInfo::FETCH_EXTRA_LAZY; + $class->associationMappings['_references']['fetch'] = ClassMetadataInfo::FETCH_EXTRA_LAZY; + $class->associationMappings['_cars']['fetch'] = ClassMetadataInfo::FETCH_EXTRA_LAZY; + + $this->loadFixture(); + } + + public function tearDown() + { + parent::tearDown(); + + $class = $this->_em->getClassMetadata('Doctrine\Tests\Models\Legacy\LegacyUser'); + $class->associationMappings['_articles']['fetch'] = ClassMetadataInfo::FETCH_LAZY; + $class->associationMappings['_references']['fetch'] = ClassMetadataInfo::FETCH_LAZY; + $class->associationMappings['_cars']['fetch'] = ClassMetadataInfo::FETCH_LAZY; + } + + public function testCountNotInitializesLegacyCollection() + { + $user = $this->_em->find('Doctrine\Tests\Models\Legacy\LegacyUser', $this->userId); + $queryCount = $this->getCurrentQueryCount(); + + $this->assertFalse($user->_articles->isInitialized()); + $this->assertEquals(2, count($user->_articles)); + $this->assertFalse($user->_articles->isInitialized()); + + foreach ($user->_articles AS $article) { } + + $this->assertEquals($queryCount + 2, $this->getCurrentQueryCount(), "Expecting two queries to be fired for count, then iteration."); + } + + public function testCountNotInitializesLegacyCollectionWithForeignIdentifier() + { + $user = $this->_em->find('Doctrine\Tests\Models\Legacy\LegacyUser', $this->userId); + $queryCount = $this->getCurrentQueryCount(); + + $this->assertFalse($user->_references->isInitialized()); + $this->assertEquals(2, count($user->_references)); + $this->assertFalse($user->_references->isInitialized()); + + foreach ($user->_references AS $reference) { } + + $this->assertEquals($queryCount + 2, $this->getCurrentQueryCount(), "Expecting two queries to be fired for count, then iteration."); + } + + public function testCountNotInitializesLegacyManyToManyCollection() + { + $user = $this->_em->find('Doctrine\Tests\Models\Legacy\LegacyUser', $this->userId); + $queryCount = $this->getCurrentQueryCount(); + + $this->assertFalse($user->_cars->isInitialized()); + $this->assertEquals(3, count($user->_cars)); + $this->assertFalse($user->_cars->isInitialized()); + + foreach ($user->_cars AS $reference) { } + + $this->assertEquals($queryCount + 2, $this->getCurrentQueryCount(), "Expecting two queries to be fired for count, then iteration."); + } + + public function loadFixture() + { + $user1 = new \Doctrine\Tests\Models\Legacy\LegacyUser(); + $user1->_username = "beberlei"; + $user1->_name = "Benjamin"; + $user1->_status = "active"; + + $user2 = new \Doctrine\Tests\Models\Legacy\LegacyUser(); + $user2->_username = "jwage"; + $user2->_name = "Jonathan"; + $user2->_status = "active"; + + $user3 = new \Doctrine\Tests\Models\Legacy\LegacyUser(); + $user3->_username = "romanb"; + $user3->_name = "Roman"; + $user3->_status = "active"; + + $this->_em->persist($user1); + $this->_em->persist($user2); + $this->_em->persist($user3); + + $article1 = new \Doctrine\Tests\Models\Legacy\LegacyArticle(); + $article1->_topic = "Test"; + $article1->_text = "Test"; + $article1->setAuthor($user1); + + $article2 = new \Doctrine\Tests\Models\Legacy\LegacyArticle(); + $article2->_topic = "Test"; + $article2->_text = "Test"; + $article2->setAuthor($user1); + + $this->_em->persist($article1); + $this->_em->persist($article2); + + $car1 = new \Doctrine\Tests\Models\Legacy\LegacyCar(); + $car1->_description = "Test1"; + + $car2 = new \Doctrine\Tests\Models\Legacy\LegacyCar(); + $car2->_description = "Test2"; + + $car3 = new \Doctrine\Tests\Models\Legacy\LegacyCar(); + $car3->_description = "Test3"; + + $user1->addCar($car1); + $user1->addCar($car2); + $user1->addCar($car3); + + $user2->addCar($car1); + $user3->addCar($car1); + + $this->_em->persist($car1); + $this->_em->persist($car2); + $this->_em->persist($car3); + + $this->_em->flush(); + + $detail1 = new \Doctrine\Tests\Models\Legacy\LegacyUserReference($user1, $user2, "foo"); + $detail2 = new \Doctrine\Tests\Models\Legacy\LegacyUserReference($user1, $user3, "bar"); + + $this->_em->persist($detail1); + $this->_em->persist($detail2); + + $this->_em->flush(); + $this->_em->clear(); + + $this->userId = $user1->getId(); + } +} diff --git a/tests/Doctrine/Tests/OrmFunctionalTestCase.php b/tests/Doctrine/Tests/OrmFunctionalTestCase.php index 876f13954..50edbfd75 100644 --- a/tests/Doctrine/Tests/OrmFunctionalTestCase.php +++ b/tests/Doctrine/Tests/OrmFunctionalTestCase.php @@ -105,6 +105,12 @@ abstract class OrmFunctionalTestCase extends OrmTestCase 'Doctrine\Tests\Models\StockExchange\Stock', 'Doctrine\Tests\Models\StockExchange\Market', ), + 'legacy' => array( + 'Doctrine\Tests\Models\Legacy\LegacyUser', + 'Doctrine\Tests\Models\Legacy\LegacyUserReference', + 'Doctrine\Tests\Models\Legacy\LegacyArticle', + 'Doctrine\Tests\Models\Legacy\LegacyCar', + ), ); protected function useModelSet($setName) @@ -204,6 +210,12 @@ abstract class OrmFunctionalTestCase extends OrmTestCase $conn->executeUpdate('DELETE FROM exchange_stocks'); $conn->executeUpdate('DELETE FROM exchange_markets'); } + if (isset($this->_usedModelSets['legacy'])) { + $conn->executeUpdate('DELETE FROM legacy_articles'); + $conn->executeUpdate('DELETE FROM legacy_cars'); + $conn->executeUpdate('DELETE FROM legacy_users'); + $conn->executeUpdate('DELETE FROM legacy_users_reference'); + } $this->_em->clear(); }