Skip to content

Commit

Permalink
Merge branch 'DDC-3719-fix' of github.com:jeanCarloMachado/doctrine2 …
Browse files Browse the repository at this point in the history
…into many-to-many-criteria-fixes
  • Loading branch information
Jeremy Giberson committed Feb 11, 2016
2 parents 788143d + 468fe31 commit f0accca
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 8 deletions.
28 changes: 20 additions & 8 deletions lib/Doctrine/ORM/Persisters/Collection/ManyToManyPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,29 +236,41 @@ public function loadCriteria(PersistentCollection $collection, Criteria $criteri
$mapping = $collection->getMapping();
$owner = $collection->getOwner();
$ownerMetadata = $this->em->getClassMetadata(get_class($owner));
$id = $this->uow->getEntityIdentifier($owner);
$targetClass = $this->em->getClassMetadata($mapping['targetEntity']);
$onConditions = $this->getOnConditionSQL($mapping);
$whereClauses = $params = array();

foreach ($mapping['relationToSourceKeyColumns'] as $key => $value) {
if ( ! $mapping['isOwningSide']) {
$associationSourceClass = $targetClass;
$mapping = $targetClass->associationMappings[$mapping['mappedBy']];
$sourceRelationMode = 'relationToTargetKeyColumns';
} else {
$associationSourceClass = $ownerMetadata;
$sourceRelationMode = 'relationToSourceKeyColumns';
}

foreach ($mapping[$sourceRelationMode] as $key => $value) {
$whereClauses[] = sprintf('t.%s = ?', $key);
$params[] = $ownerMetadata->getFieldValue($owner, $value);
$params[] = $ownerMetadata->containsForeignIdentifier
? $id[$ownerMetadata->getFieldForColumn($value)]
: $id[$ownerMetadata->fieldNames[$value]];
}

$parameters = $this->expandCriteriaParameters($criteria);

foreach ($parameters as $parameter) {
list($name, $value) = $parameter;
$whereClauses[] = sprintf('te.%s = ?', $name);
$field = $this->quoteStrategy->getColumnName($name, $targetClass, $this->platform);
$whereClauses[] = sprintf('te.%s = ?', $field);
$params[] = $value;
}

$mapping = $collection->getMapping();
$targetClass = $this->em->getClassMetadata($mapping['targetEntity']);
$tableName = $this->quoteStrategy->getTableName($targetClass, $this->platform);
$joinTable = $this->quoteStrategy->getJoinTableName($mapping, $ownerMetadata, $this->platform);
$onConditions = $this->getOnConditionSQL($mapping);
$joinTable = $this->quoteStrategy->getJoinTableName($mapping, $associationSourceClass, $this->platform);

$rsm = new Query\ResultSetMappingBuilder($this->em);
$rsm->addRootEntityFromClassMetadata($mapping['targetEntity'], 'te');
$rsm->addRootEntityFromClassMetadata($targetClass->name, 'te');

$sql = 'SELECT ' . $rsm->generateSelectClause()
. ' FROM ' . $tableName . ' te'
Expand Down
54 changes: 54 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3719Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Doctrine\Tests\ORM\Functional\Ticket;


use Doctrine\Common\Collections\Criteria;
use Doctrine\Tests\Models\Company\CompanyFlexContract;
use Doctrine\Tests\Models\Company\CompanyManager;

/**
* @author Jean Carlo Machado <[email protected]>
*/
class DDC3719Test extends \Doctrine\Tests\OrmFunctionalTestCase
{
public function setUp()
{
$this->useModelSet('company');
parent::setUp();
}

/**
* @group DDC-3719
*/
public function testCriteriaOnNotOwningSide()
{
$manager = new CompanyManager();
$manager->setName('Gandalf');
$manager->setSalary(666);
$manager->setTitle('Boss');
$manager->setDepartment('Marketing');
$this->_em->persist($manager);

$contractA = new CompanyFlexContract();
$contractA->markCompleted();
$contractA->addManager($manager);
$this->_em->persist($contractA);

$contractB = new CompanyFlexContract();
$contractB->addManager($manager);
$this->_em->persist($contractB);

$this->_em->flush();
$this->_em->refresh($manager);

$contracts = $manager->managedContracts;
static::assertCount(2, $contracts);

$criteria = Criteria::create();
$criteria->where(Criteria::expr()->eq("completed", true));

$completedContracts = $contracts->matching($criteria);
static::assertCount(1, $completedContracts);
}
}

0 comments on commit f0accca

Please sign in to comment.