From 24a7a72f59f807034ccaa3695f2da67dd2f33d82 Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Thu, 31 Mar 2011 23:31:58 +0200 Subject: [PATCH 1/2] [DDC-991] add AbstractQuery::getOneResult() method that returns null instead of throwing an exception as getSingleResult() does. --- lib/Doctrine/ORM/AbstractQuery.php | 27 ++++++++- .../Tests/ORM/Functional/QueryTest.php | 60 +++++++++++++++++++ 2 files changed, 85 insertions(+), 2 deletions(-) diff --git a/lib/Doctrine/ORM/AbstractQuery.php b/lib/Doctrine/ORM/AbstractQuery.php index bdad77973..628c09c7d 100644 --- a/lib/Doctrine/ORM/AbstractQuery.php +++ b/lib/Doctrine/ORM/AbstractQuery.php @@ -1,7 +1,5 @@ execute(array(), self::HYDRATE_SCALAR); } + /** + * Get exactly one result or null. + * + * @throws NonUniqueResultException + * @param int $hydrationMode + * @return mixed + */ + public function getOneResult($hydrationMode = null) + { + $result = $this->execute(array(), $hydrationMode); + + if ($this->_hydrationMode !== self::HYDRATE_SINGLE_SCALAR && ! $result) { + return null; + } + + if (is_array($result)) { + if (count($result) > 1) { + throw new NonUniqueResultException; + } + return array_shift($result); + } + + return $result; + } + /** * Gets the single result of the query. * diff --git a/tests/Doctrine/Tests/ORM/Functional/QueryTest.php b/tests/Doctrine/Tests/ORM/Functional/QueryTest.php index 567b3ff8e..433c902d9 100644 --- a/tests/Doctrine/Tests/ORM/Functional/QueryTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/QueryTest.php @@ -378,4 +378,64 @@ class QueryTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->assertNotInstanceOf('Doctrine\ORM\Proxy\Proxy', $article); } } + + /** + * @group DDC-991 + */ + public function testGetOneResult() + { + $user = new CmsUser; + $user->name = 'Guilherme'; + $user->username = 'gblanco'; + $user->status = 'developer'; + $this->_em->persist($user); + $this->_em->flush(); + $this->_em->clear(); + + $query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username = 'gblanco'"); + + $fetchedUser = $query->getOneResult(); + $this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsUser', $fetchedUser); + $this->assertEquals('gblanco', $fetchedUser->username); + + $query = $this->_em->createQuery("select u.username from Doctrine\Tests\Models\CMS\CmsUser u where u.username = 'gblanco'"); + $fetchedUsername = $query->getOneResult(Query::HYDRATE_SINGLE_SCALAR); + $this->assertEquals('gblanco', $fetchedUsername); + } + + /** + * @group DDC-991 + */ + public function testGetOneResultSeveralRows() + { + $user = new CmsUser; + $user->name = 'Guilherme'; + $user->username = 'gblanco'; + $user->status = 'developer'; + $this->_em->persist($user); + $user = new CmsUser; + $user->name = 'Roman'; + $user->username = 'romanb'; + $user->status = 'developer'; + $this->_em->persist($user); + $this->_em->flush(); + $this->_em->clear(); + + $query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u"); + + $this->setExpectedException('Doctrine\ORM\NonUniqueResultException'); + $fetchedUser = $query->getOneResult(); + } + + /** + * @group DDC-991 + */ + public function testGetOneResultNoRows() + { + $query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u"); + $this->assertNull($query->getOneResult()); + + $query = $this->_em->createQuery("select u.username from Doctrine\Tests\Models\CMS\CmsUser u where u.username = 'gblanco'"); + $this->assertNull($query->getOneResult(Query::HYDRATE_SCALAR)); + } } \ No newline at end of file From ea52b3cc8f773ddeb00ad4669d94584b9b11d90d Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Thu, 31 Mar 2011 23:35:01 +0200 Subject: [PATCH 2/2] [DDC-991] Rename method to AbstractQuery::getOneOrNullResult(). --- lib/Doctrine/ORM/AbstractQuery.php | 2 +- .../Doctrine/Tests/ORM/Functional/QueryTest.php | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/Doctrine/ORM/AbstractQuery.php b/lib/Doctrine/ORM/AbstractQuery.php index 628c09c7d..b52921644 100644 --- a/lib/Doctrine/ORM/AbstractQuery.php +++ b/lib/Doctrine/ORM/AbstractQuery.php @@ -420,7 +420,7 @@ abstract class AbstractQuery * @param int $hydrationMode * @return mixed */ - public function getOneResult($hydrationMode = null) + public function getOneOrNullResult($hydrationMode = null) { $result = $this->execute(array(), $hydrationMode); diff --git a/tests/Doctrine/Tests/ORM/Functional/QueryTest.php b/tests/Doctrine/Tests/ORM/Functional/QueryTest.php index 433c902d9..02b4d267e 100644 --- a/tests/Doctrine/Tests/ORM/Functional/QueryTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/QueryTest.php @@ -382,7 +382,7 @@ class QueryTest extends \Doctrine\Tests\OrmFunctionalTestCase /** * @group DDC-991 */ - public function testGetOneResult() + public function testgetOneOrNullResult() { $user = new CmsUser; $user->name = 'Guilherme'; @@ -394,19 +394,19 @@ class QueryTest extends \Doctrine\Tests\OrmFunctionalTestCase $query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username = 'gblanco'"); - $fetchedUser = $query->getOneResult(); + $fetchedUser = $query->getOneOrNullResult(); $this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsUser', $fetchedUser); $this->assertEquals('gblanco', $fetchedUser->username); $query = $this->_em->createQuery("select u.username from Doctrine\Tests\Models\CMS\CmsUser u where u.username = 'gblanco'"); - $fetchedUsername = $query->getOneResult(Query::HYDRATE_SINGLE_SCALAR); + $fetchedUsername = $query->getOneOrNullResult(Query::HYDRATE_SINGLE_SCALAR); $this->assertEquals('gblanco', $fetchedUsername); } /** * @group DDC-991 */ - public function testGetOneResultSeveralRows() + public function testgetOneOrNullResultSeveralRows() { $user = new CmsUser; $user->name = 'Guilherme'; @@ -424,18 +424,18 @@ class QueryTest extends \Doctrine\Tests\OrmFunctionalTestCase $query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u"); $this->setExpectedException('Doctrine\ORM\NonUniqueResultException'); - $fetchedUser = $query->getOneResult(); + $fetchedUser = $query->getOneOrNullResult(); } /** * @group DDC-991 */ - public function testGetOneResultNoRows() + public function testgetOneOrNullResultNoRows() { $query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u"); - $this->assertNull($query->getOneResult()); + $this->assertNull($query->getOneOrNullResult()); $query = $this->_em->createQuery("select u.username from Doctrine\Tests\Models\CMS\CmsUser u where u.username = 'gblanco'"); - $this->assertNull($query->getOneResult(Query::HYDRATE_SCALAR)); + $this->assertNull($query->getOneOrNullResult(Query::HYDRATE_SCALAR)); } } \ No newline at end of file