From af2295b73aae83aca5047403cdb6c3e7f4bc32c2 Mon Sep 17 00:00:00 2001 From: Wojciech Zylinski Date: Sun, 3 May 2015 18:39:51 +0100 Subject: [PATCH] Switch to relationToTargetKeyColumns when matching non-owning side with Criteria. Fixes DDC-3719. --- .../Collection/ManyToManyPersister.php | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/lib/Doctrine/ORM/Persisters/Collection/ManyToManyPersister.php b/lib/Doctrine/ORM/Persisters/Collection/ManyToManyPersister.php index bf9f14e68..6b5dd554e 100644 --- a/lib/Doctrine/ORM/Persisters/Collection/ManyToManyPersister.php +++ b/lib/Doctrine/ORM/Persisters/Collection/ManyToManyPersister.php @@ -229,29 +229,41 @@ class ManyToManyPersister extends AbstractCollectionPersister $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'