1
0
mirror of synced 2025-01-18 14:31:40 +03:00

DDC-546 - Fix bug in inverse many-to-many contains.

This commit is contained in:
Benjamin Eberlei 2011-01-02 13:37:29 +01:00
parent 685e327b43
commit 3acc05d953
2 changed files with 37 additions and 8 deletions

View File

@ -247,16 +247,21 @@ class ManyToManyPersister extends AbstractCollectionPersister
$params = array();
$mapping = $coll->getMapping();
$sourceClass = $this->_em->getClassMetadata($mapping['sourceEntity']);
$elementClass = $this->_em->getClassMetadata($mapping['targetEntity']);
$sourceId = $uow->getEntityIdentifier($coll->getOwner());
$elementId = $uow->getEntityIdentifier($element);
if ($mapping['isOwningSide']) {
$joinTable = $mapping['joinTable'];
if (!$mapping['isOwningSide']) {
$sourceClass = $this->_em->getClassMetadata($mapping['targetEntity']);
$targetClass = $this->_em->getClassMetadata($mapping['sourceEntity']);
$sourceId = $uow->getEntityIdentifier($element);
$targetId = $uow->getEntityIdentifier($coll->getOwner());
$mapping = $sourceClass->associationMappings[$mapping['mappedBy']];
} else {
$joinTable = $elementClass->associationMappings[$mapping['mappedBy']]['joinTable'];
$sourceClass = $this->_em->getClassMetadata($mapping['sourceEntity']);
$targetClass = $this->_em->getClassMetadata($mapping['targetEntity']);
$sourceId = $uow->getEntityIdentifier($coll->getOwner());
$targetId = $uow->getEntityIdentifier($element);
}
$joinTable = $mapping['joinTable'];
$whereClause = '';
foreach ($mapping['joinTableColumns'] as $joinTableColumn) {
@ -266,7 +271,7 @@ class ManyToManyPersister extends AbstractCollectionPersister
}
$whereClause .= "$joinTableColumn = ?";
$params[] = $elementId[$sourceClass->fieldNames[$mapping['relationToTargetKeyColumns'][$joinTableColumn]]];
$params[] = $targetId[$sourceClass->fieldNames[$mapping['relationToTargetKeyColumns'][$joinTableColumn]]];
} else if (isset($mapping['relationToSourceKeyColumns'][$joinTableColumn])) {
if ($whereClause !== '') {
$whereClause .= ' AND ';

View File

@ -280,6 +280,30 @@ class ExtraLazyCollectionTest extends \Doctrine\Tests\OrmFunctionalTestCase
$this->assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
}
/**
* @group DDC-546
*/
public function testContainsManyToManyInverse()
{
$group = $this->_em->find('Doctrine\Tests\Models\CMS\CmsGroup', $this->groupId);
$this->assertFalse($group->users->isInitialized(), "Pre-Condition: Collection is not initialized.");
$user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
$queryCount = $this->getCurrentQueryCount();
$this->assertTrue($group->users->contains($user));
$this->assertEquals($queryCount+1, $this->getCurrentQueryCount(), "Checking for contains of managed entity should cause one query to be executed.");
$this->assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
$newUser = new \Doctrine\Tests\Models\CMS\CmsUser();
$newUser->name = "A New group!";
$queryCount = $this->getCurrentQueryCount();
$this->assertFalse($group->users->contains($newUser));
$this->assertEquals($queryCount, $this->getCurrentQueryCount(), "Checking for contains of new entity should cause no query to be executed.");
$this->assertFalse($user->groups->isInitialized(), "Post-Condition: Collection is not initialized.");
}
private function loadFixture()
{
$user1 = new \Doctrine\Tests\Models\CMS\CmsUser();