From 9385a600cfa363d3613ef5ba2f84c1078b474026 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Gallego?= Date: Sun, 22 Dec 2013 17:00:56 +0100 Subject: [PATCH] Fix wrong logic --- lib/Doctrine/ORM/PersistentCollection.php | 2 +- .../ORM/Persisters/ManyToManyPersister.php | 21 +++++++++++++------ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/lib/Doctrine/ORM/PersistentCollection.php b/lib/Doctrine/ORM/PersistentCollection.php index 90890e906..056f455c5 100644 --- a/lib/Doctrine/ORM/PersistentCollection.php +++ b/lib/Doctrine/ORM/PersistentCollection.php @@ -876,7 +876,7 @@ final class PersistentCollection implements Collection, Selectable if ($this->association['type'] === ClassMetadata::MANY_TO_MANY) { $persister = $this->em->getUnitOfWork()->getCollectionPersister($this->association); - return new ArrayCollection($persister->loadCriteria($this, $this->owner, $criteria)); + return new ArrayCollection($persister->loadCriteria($this, $criteria)); } $builder = Criteria::expr(); diff --git a/lib/Doctrine/ORM/Persisters/ManyToManyPersister.php b/lib/Doctrine/ORM/Persisters/ManyToManyPersister.php index 38a825375..d4bb6f907 100644 --- a/lib/Doctrine/ORM/Persisters/ManyToManyPersister.php +++ b/lib/Doctrine/ORM/Persisters/ManyToManyPersister.php @@ -556,29 +556,38 @@ class ManyToManyPersister extends AbstractCollectionPersister * Loads Entities matching the given Criteria object. * * @param PersistentCollection $coll - * @param object $owner * @param \Doctrine\Common\Collections\Criteria $criteria * @return array */ - public function loadCriteria(PersistentCollection $coll, $owner, Criteria $criteria) + public function loadCriteria(PersistentCollection $coll, Criteria $criteria) { - list($quotedJoinTable, $whereClauses, $params) = $this->getJoinTableRestrictions($coll, $owner, true); + $mapping = $coll->getMapping(); + $owner = $coll->getOwner(); + $ownerMetadata = $this->em->getClassMetadata(get_class($owner)); + + $whereClauses = $params = array(); + + foreach ($mapping['relationToSourceKeyColumns'] as $key => $value) { + $whereClauses[] = sprintf('t.%s = ?', $key); + $params[] = $ownerMetadata->getFieldValue($owner, $value); + } $parameters = $this->expandCriteriaParameters($criteria); foreach ($parameters as $parameter) { list($name, $value) = $parameter; - $whereClauses[] = sprintf("te.%s = ?", $name); - $params[] = $value; + $whereClauses[] = sprintf("te.%s = ?", $name); + $params[] = $value; } $mapping = $coll->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); $sql = 'SELECT * FROM ' . $tableName . ' te' - . ' JOIN ' . $quotedJoinTable . ' ON' + . ' JOIN ' . $joinTable . ' t ON' . implode(' AND ', $onConditions) . ' WHERE ' . implode(' AND ', $whereClauses);