From d7dbde8f3e2ecbc341e8de47127d0706411bb910 Mon Sep 17 00:00:00 2001 From: Alexander Date: Thu, 28 Jul 2011 11:01:52 +0200 Subject: [PATCH] [DDC-1301] Fixed count() for fetch="EXTRA_LAZY" on OneToMany association --- .../ORM/Persisters/OneToManyPersister.php | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) 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 +}