From 3b3186ee98392802a44118cd421a3530119aa7ea Mon Sep 17 00:00:00 2001 From: Guilherme Blanco Date: Thu, 1 Sep 2011 19:11:57 -0300 Subject: [PATCH] Added support to user provide an array of Entities as a DQL parameter. Fixes DDC-1356. --- lib/Doctrine/ORM/Query.php | 110 ++++++++++++------ .../Tests/ORM/Functional/QueryTest.php | 32 +++++ 2 files changed, 104 insertions(+), 38 deletions(-) diff --git a/lib/Doctrine/ORM/Query.php b/lib/Doctrine/ORM/Query.php index 820711d55..8dfd60b4f 100644 --- a/lib/Doctrine/ORM/Query.php +++ b/lib/Doctrine/ORM/Query.php @@ -238,50 +238,84 @@ final class Query extends AbstractQuery throw QueryException::invalidParameterNumber(); } - $sqlParams = $types = array(); - - foreach ($this->_params as $key => $value) { - if ( ! isset($paramMappings[$key])) { - throw QueryException::unknownParameter($key); - } - if (isset($this->_paramTypes[$key])) { - foreach ($paramMappings[$key] as $position) { - $types[$position] = $this->_paramTypes[$key]; - } - } - - if (is_object($value) && $this->_em->getMetadataFactory()->hasMetadataFor(get_class($value))) { - if ($this->_em->getUnitOfWork()->getEntityState($value) == UnitOfWork::STATE_MANAGED) { - $idValues = $this->_em->getUnitOfWork()->getEntityIdentifier($value); - } else { - $class = $this->_em->getClassMetadata(get_class($value)); - $idValues = $class->getIdentifierValues($value); - } - $sqlPositions = $paramMappings[$key]; - $cSqlPos = count($sqlPositions); - $cIdValues = count($idValues); - $idValues = array_values($idValues); - for ($i = 0; $i < $cSqlPos; $i++) { - $sqlParams[$sqlPositions[$i]] = $idValues[ ($i % $cIdValues) ]; - } - } else { - foreach ($paramMappings[$key] as $position) { - $sqlParams[$position] = $value; - } - } - } - - if ($sqlParams) { - ksort($sqlParams); - $sqlParams = array_values($sqlParams); - } - + list($sqlParams, $types) = $this->processParameterMappings($paramMappings); + if ($this->_resultSetMapping === null) { $this->_resultSetMapping = $this->_parserResult->getResultSetMapping(); } return $executor->execute($this->_em->getConnection(), $sqlParams, $types); } + + /** + * Processes query parameter mappings + * + * @param array $paramMappings + * @return array + */ + private function processParameterMappings($paramMappings) + { + $sqlParams = $types = array(); + + foreach ($this->_params as $key => $value) { + if ( ! isset($paramMappings[$key])) { + throw QueryException::unknownParameter($key); + } + + if (isset($this->_paramTypes[$key])) { + foreach ($paramMappings[$key] as $position) { + $types[$position] = $this->_paramTypes[$key]; + } + } + + $sqlPositions = $paramMappings[$key]; + $value = array_values($this->processParameterValue($value)); + $countValue = count($value); + + for ($i = 0, $l = count($sqlPositions); $i < $l; $i++) { + $sqlParams[$sqlPositions[$i]] = $value[($i % $countValue)]; + } + } + + if ($sqlParams) { + ksort($sqlParams); + $sqlParams = array_values($sqlParams); + } + + return array($sqlParams, $types); + } + + /** + * Process an individual parameter value + * + * @param mixed $value + * @return array + */ + private function processParameterValue($value) + { + if (is_array($value)) { + for ($i = 0, $l = count($value); $i < $l; $i++) { + $paramValue = $this->processParameterValue($value[$i]); + + // TODO: What about Entities that have composite primary key? + $value[$i] = is_array($paramValue) ? $paramValue[key($paramValue)] : $paramValue; + } + + return array($value); + } + + if ( ! (is_object($value) && $this->_em->getMetadataFactory()->hasMetadataFor(get_class($value)))) { + return array($value); + } + + if ($this->_em->getUnitOfWork()->getEntityState($value) === UnitOfWork::STATE_MANAGED) { + return array_values($this->_em->getUnitOfWork()->getEntityIdentifier($value)); + } + + $class = $this->_em->getClassMetadata(get_class($value)); + + return array_values($class->getIdentifierValues($value)); + } /** * Defines a cache driver to be used for caching queries. diff --git a/tests/Doctrine/Tests/ORM/Functional/QueryTest.php b/tests/Doctrine/Tests/ORM/Functional/QueryTest.php index 82180a4ed..993ce1642 100644 --- a/tests/Doctrine/Tests/ORM/Functional/QueryTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/QueryTest.php @@ -501,4 +501,36 @@ class QueryTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->assertEquals(0, count($users)); } + + public function testQueryWithArrayOfEntitiesAsParameter() + { + $userA = new CmsUser; + $userA->name = 'Benjamin'; + $userA->username = 'beberlei'; + $userA->status = 'developer'; + $this->_em->persist($userA); + + $userB = new CmsUser; + $userB->name = 'Roman'; + $userB->username = 'romanb'; + $userB->status = 'developer'; + $this->_em->persist($userB); + + $userC = new CmsUser; + $userC->name = 'Jonathan'; + $userC->username = 'jwage'; + $userC->status = 'developer'; + $this->_em->persist($userC); + + $this->_em->flush(); + $this->_em->clear(); + + $query = $this->_em->createQuery("SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u IN (?0) OR u.username = ?1"); + $query->setParameter(0, array($userA, $userC)); + $query->setParameter(1, 'beberlei'); + + $users = $query->execute(); + + $this->assertEquals(2, count($users)); + } } \ No newline at end of file