From 632d13ba0c8f909cdb44c3890244b41c9f047ff2 Mon Sep 17 00:00:00 2001 From: "Fabio B. Silva" Date: Wed, 27 Jun 2012 14:15:06 -0300 Subject: [PATCH] fix extra lazy count --- .../ORM/Persisters/BasicEntityPersister.php | 2 +- .../ORM/Persisters/ManyToManyPersister.php | 49 ++++++++++--------- .../ORM/Functional/Ticket/DDC1885Test.php | 17 ++++++- 3 files changed, 42 insertions(+), 26 deletions(-) diff --git a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php index 8b54271c9..2d9b1f45c 100644 --- a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php +++ b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php @@ -1160,7 +1160,7 @@ class BasicEntityPersister $conditions[] = $sourceTableAlias . '.' . $quotedTargetColumn . ' = ' . $joinTableName . '.' . $quotedSourceColumn; } - return ' INNER JOIN ' . $joinTableName . ' ON ' . implode(' AND ', $conditions); + return ' INNER JOIN ' . $joinTableName . ' ON ' . implode(' AND ', $conditions); } /** diff --git a/lib/Doctrine/ORM/Persisters/ManyToManyPersister.php b/lib/Doctrine/ORM/Persisters/ManyToManyPersister.php index db2792152..23e41786c 100644 --- a/lib/Doctrine/ORM/Persisters/ManyToManyPersister.php +++ b/lib/Doctrine/ORM/Persisters/ManyToManyPersister.php @@ -212,41 +212,42 @@ class ManyToManyPersister extends AbstractCollectionPersister */ public function count(PersistentCollection $coll) { - $mapping = $filterMapping = $coll->getMapping(); - $class = $this->_em->getClassMetadata($mapping['sourceEntity']); - $id = $this->_em->getUnitOfWork()->getEntityIdentifier($coll->getOwner()); + $conditions = array(); + $params = array(); + $mapping = $coll->getMapping(); + $association = $mapping; + $class = $this->_em->getClassMetadata($mapping['sourceEntity']); + $id = $this->_em->getUnitOfWork()->getEntityIdentifier($coll->getOwner()); - if ($mapping['isOwningSide']) { - $joinColumns = $mapping['relationToSourceKeyColumns']; - } else { - $mapping = $this->_em->getClassMetadata($mapping['targetEntity'])->associationMappings[$mapping['mappedBy']]; - $joinColumns = $mapping['relationToTargetKeyColumns']; + if ( ! $mapping['isOwningSide']) { + $targetEntity = $this->_em->getClassMetadata($mapping['targetEntity']); + $association = $targetEntity->associationMappings[$mapping['mappedBy']]; } - $whereClauses = array(); - $params = array(); + $joinColumns = ( ! $mapping['isOwningSide']) + ? $association['joinTable']['inverseJoinColumns'] + : $association['joinTable']['joinColumns']; - foreach ($mapping['joinTableColumns'] as $joinTableColumn) { - if ( ! isset($joinColumns[$joinTableColumn])) { - continue; - } - - $whereClauses[] = $joinTableColumn . ' = ?'; - - $params[] = ($class->containsForeignIdentifier) - ? $id[$class->getFieldForColumn($joinColumns[$joinTableColumn])] - : $id[$class->fieldNames[$joinColumns[$joinTableColumn]]]; + foreach ($joinColumns as $joinColumn) { + $columnName = $this->quoteStrategy->getJoinColumnName($joinColumn, $class, $this->platform); + $referencedName = $joinColumn['referencedColumnName']; + $conditions[] = $columnName . ' = ?'; + $params[] = ($class->containsForeignIdentifier) + ? $id[$class->getFieldForColumn($referencedName)] + : $id[$class->fieldNames[$referencedName]]; } - list($joinTargetEntitySQL, $filterSql) = $this->getFilterSql($filterMapping); + $joinTableName = $this->quoteStrategy->getJoinTableName($association, $class, $this->platform); + list($joinTargetEntitySQL, $filterSql) = $this->getFilterSql($mapping); + if ($filterSql) { - $whereClauses[] = $filterSql; + $conditions[] = $filterSql; } $sql = 'SELECT COUNT(*)' - . ' FROM ' . $this->quoteStrategy->getJoinTableName($mapping, $class, $this->platform) . ' t' + . ' FROM ' . $joinTableName . ' t' . $joinTargetEntitySQL - . ' WHERE ' . implode(' AND ', $whereClauses); + . ' WHERE ' . implode(' AND ', $conditions); return $this->_conn->fetchColumn($sql, $params); } diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1885Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1885Test.php index 09b55fbc4..8fbb19ad0 100644 --- a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1885Test.php +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1885Test.php @@ -126,7 +126,7 @@ class DDC1885Test extends \Doctrine\Tests\OrmFunctionalTestCase $this->assertCount(1, $user->getGroups()); } - public function testClearAll() + public function testClearAll() { $user = $this->user; $u1Id = $user->id; @@ -155,4 +155,19 @@ class DDC1885Test extends \Doctrine\Tests\OrmFunctionalTestCase $this->assertCount(0, $user->getGroups()); } + + public function testCountExtraLazy() + { + $user = $this->user; + $u1Id = $user->id; + $user = $this->_em->find('Doctrine\Tests\Models\Quote\User', $u1Id); + + $this->assertInstanceOf('Doctrine\Tests\Models\Quote\User', $user); + $this->assertEquals('FabioBatSilva', $user->name); + $this->assertEquals($u1Id, $user->id); + + $this->assertCount(0, $user->extraLazyGroups); + $this->assertInstanceOf('Doctrine\Tests\Models\Quote\Group', $user->getGroups()->get(0)); + $this->assertInstanceOf('Doctrine\Tests\Models\Quote\Group', $user->getGroups()->get(1)); + } } \ No newline at end of file