1
0
mirror of synced 2025-01-19 06:51:40 +03:00

fix extra lazy count

This commit is contained in:
Fabio B. Silva 2012-06-27 14:15:06 -03:00
parent fe11831bd7
commit 632d13ba0c
3 changed files with 42 additions and 26 deletions

View File

@ -212,41 +212,42 @@ class ManyToManyPersister extends AbstractCollectionPersister
*/ */
public function count(PersistentCollection $coll) public function count(PersistentCollection $coll)
{ {
$mapping = $filterMapping = $coll->getMapping(); $conditions = array();
$params = array();
$mapping = $coll->getMapping();
$association = $mapping;
$class = $this->_em->getClassMetadata($mapping['sourceEntity']); $class = $this->_em->getClassMetadata($mapping['sourceEntity']);
$id = $this->_em->getUnitOfWork()->getEntityIdentifier($coll->getOwner()); $id = $this->_em->getUnitOfWork()->getEntityIdentifier($coll->getOwner());
if ($mapping['isOwningSide']) { if ( ! $mapping['isOwningSide']) {
$joinColumns = $mapping['relationToSourceKeyColumns']; $targetEntity = $this->_em->getClassMetadata($mapping['targetEntity']);
} else { $association = $targetEntity->associationMappings[$mapping['mappedBy']];
$mapping = $this->_em->getClassMetadata($mapping['targetEntity'])->associationMappings[$mapping['mappedBy']];
$joinColumns = $mapping['relationToTargetKeyColumns'];
} }
$whereClauses = array(); $joinColumns = ( ! $mapping['isOwningSide'])
$params = array(); ? $association['joinTable']['inverseJoinColumns']
: $association['joinTable']['joinColumns'];
foreach ($mapping['joinTableColumns'] as $joinTableColumn) {
if ( ! isset($joinColumns[$joinTableColumn])) {
continue;
}
$whereClauses[] = $joinTableColumn . ' = ?';
foreach ($joinColumns as $joinColumn) {
$columnName = $this->quoteStrategy->getJoinColumnName($joinColumn, $class, $this->platform);
$referencedName = $joinColumn['referencedColumnName'];
$conditions[] = $columnName . ' = ?';
$params[] = ($class->containsForeignIdentifier) $params[] = ($class->containsForeignIdentifier)
? $id[$class->getFieldForColumn($joinColumns[$joinTableColumn])] ? $id[$class->getFieldForColumn($referencedName)]
: $id[$class->fieldNames[$joinColumns[$joinTableColumn]]]; : $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) { if ($filterSql) {
$whereClauses[] = $filterSql; $conditions[] = $filterSql;
} }
$sql = 'SELECT COUNT(*)' $sql = 'SELECT COUNT(*)'
. ' FROM ' . $this->quoteStrategy->getJoinTableName($mapping, $class, $this->platform) . ' t' . ' FROM ' . $joinTableName . ' t'
. $joinTargetEntitySQL . $joinTargetEntitySQL
. ' WHERE ' . implode(' AND ', $whereClauses); . ' WHERE ' . implode(' AND ', $conditions);
return $this->_conn->fetchColumn($sql, $params); return $this->_conn->fetchColumn($sql, $params);
} }

View File

@ -155,4 +155,19 @@ class DDC1885Test extends \Doctrine\Tests\OrmFunctionalTestCase
$this->assertCount(0, $user->getGroups()); $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));
}
} }