From 1032a16db29f1e6a68b9b1a8faa866984fa8045f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Perrin?= Date: Mon, 25 Feb 2013 16:47:49 +0100 Subject: [PATCH] Simpler way to handle Collection parameters in DQL queries (refs #DDC-2319) --- lib/Doctrine/ORM/AbstractQuery.php | 7 ++- .../Tests/ORM/Functional/QueryTest.php | 49 +++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/lib/Doctrine/ORM/AbstractQuery.php b/lib/Doctrine/ORM/AbstractQuery.php index 3ac4cf840..5390c96d2 100644 --- a/lib/Doctrine/ORM/AbstractQuery.php +++ b/lib/Doctrine/ORM/AbstractQuery.php @@ -20,6 +20,7 @@ namespace Doctrine\ORM; use Doctrine\Common\Util\ClassUtils; +use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ORM\Query\Parameter; @@ -405,6 +406,10 @@ abstract class AbstractQuery return $value; } + if ($value instanceof Collection) { + $value = $value->toArray(); + } + if (is_array($value)) { foreach ($value as $key => $paramValue) { $paramValue = $this->processParameterValue($paramValue); @@ -1089,7 +1094,7 @@ abstract class AbstractQuery /** * Generates a string of currently query to use for the cache second level cache. - * + * * @return string */ protected function getHash() diff --git a/tests/Doctrine/Tests/ORM/Functional/QueryTest.php b/tests/Doctrine/Tests/ORM/Functional/QueryTest.php index 906b5e68f..0cba58413 100644 --- a/tests/Doctrine/Tests/ORM/Functional/QueryTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/QueryTest.php @@ -676,6 +676,55 @@ class QueryTest extends \Doctrine\Tests\OrmFunctionalTestCase $q->getResult(); } + /** + * @group DDC-2319 + */ + public function testSetCollectionParameterBindingSingleIdentifierObject() + { + $u1 = new CmsUser; + $u1->name = 'Name1'; + $u1->username = 'username1'; + $u1->status = 'developer'; + $this->_em->persist($u1); + + $u2 = new CmsUser; + $u2->name = 'Name2'; + $u2->username = 'username2'; + $u2->status = 'tester'; + $this->_em->persist($u2); + + $u3 = new CmsUser; + $u3->name = 'Name3'; + $u3->username = 'username3'; + $u3->status = 'tester'; + $this->_em->persist($u3); + + $this->_em->flush(); + $this->_em->clear(); + + $userCollection = new ArrayCollection(); + + $userCollection->add($u1); + $userCollection->add($u2); + $userCollection->add($u3->getId()); + + $q = $this->_em->createQuery("SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u IN (:users) ORDER BY u.id"); + $q->setParameter('users', $userCollection); + $users = $q->execute(); + + $this->assertEquals(3, count($users)); + $this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsUser', $users[0]); + $this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsUser', $users[1]); + $this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsUser', $users[2]); + + $resultUser1 = $users[0]; + $resultUser2 = $users[1]; + $resultUser3 = $users[2]; + + $this->assertEquals($u1->username, $resultUser1->username); + $this->assertEquals($u2->username, $resultUser2->username); + $this->assertEquals($u3->username, $resultUser3->username); + } /** * @group DDC-1822