1
0
mirror of synced 2025-01-18 22:41:43 +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

@ -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);
}
/**

View File

@ -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);
}

View File

@ -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));
}
}