1
0
mirror of synced 2024-12-13 22:56:04 +03:00

Merge branch 'DDC-991'

This commit is contained in:
Benjamin Eberlei 2011-03-31 23:35:12 +02:00
commit db82ef3e61
2 changed files with 85 additions and 2 deletions

View File

@ -1,7 +1,5 @@
<?php
/*
* $Id: Abstract.php 1393 2008-03-06 17:49:16Z guilhermeblanco $
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
@ -415,6 +413,31 @@ abstract class AbstractQuery
return $this->execute(array(), self::HYDRATE_SCALAR);
}
/**
* Get exactly one result or null.
*
* @throws NonUniqueResultException
* @param int $hydrationMode
* @return mixed
*/
public function getOneOrNullResult($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.
*

View File

@ -378,4 +378,64 @@ class QueryTest extends \Doctrine\Tests\OrmFunctionalTestCase
$this->assertNotInstanceOf('Doctrine\ORM\Proxy\Proxy', $article);
}
}
/**
* @group DDC-991
*/
public function testgetOneOrNullResult()
{
$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->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->getOneOrNullResult(Query::HYDRATE_SINGLE_SCALAR);
$this->assertEquals('gblanco', $fetchedUsername);
}
/**
* @group DDC-991
*/
public function testgetOneOrNullResultSeveralRows()
{
$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->getOneOrNullResult();
}
/**
* @group DDC-991
*/
public function testgetOneOrNullResultNoRows()
{
$query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u");
$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->getOneOrNullResult(Query::HYDRATE_SCALAR));
}
}