AbstractQuery::getSingleScalarResult() throws NoResultException, even though it shouldn't #6342

Open
opened 2026-01-22 15:31:25 +01:00 by admin · 3 comments
Owner

Originally created by @Piskvor on GitHub (Nov 14, 2019).

Bug Report

Q A
BC Break no
Version >=2.6.3

Summary

AbstractQuery::getSingleScalarResult() throws NoResultException, even though it shouldn't (by documentation and by code's apparent intent).

Current behavior

Calling getSingleScalarResult() on a query that returns an empty result set throws NoResultException.

How to reproduce

/**
 *  @ORM\Entity(repositoryClass="FooRepository")
 * // simplified
 */
class Foo
{
   /**
    * @ORM\Column(type="string")
    */
   public $id;
   /**
    * @ORM\Column(type="string")
    */
   public $name;
}

class FooRepository extends BaseRepository
{
    public function findIdByName(string $customName): ?string
    {
        $qb = $this->createQueryBuilder('foo')
            ->select('foo.id')
            ->andWhere('foo.name = :name')
            ->setParameter('name', $customName)
            ->setMaxResults(1);

        try {
            return $qb->getQuery()->getSingleScalarResult();
        } catch (NoResultException $exception) {
            // this try/catch should not be needed at all
            return null;
        }
    }
}

Expected behavior

Calling getSingleScalarResult() on a query that returns an empty result set returns NULL.

PR: https://github.com/doctrine/orm/pull/7895

Originally created by @Piskvor on GitHub (Nov 14, 2019). ### Bug Report | Q | A |------------ | ------ | BC Break | no | Version | >=2.6.3 #### Summary AbstractQuery::getSingleScalarResult() throws NoResultException, even though it shouldn't (by documentation and by code's apparent intent). #### Current behavior Calling getSingleScalarResult() on a query that returns an empty result set throws NoResultException. #### How to reproduce ``` /** * @ORM\Entity(repositoryClass="FooRepository") * // simplified */ class Foo { /** * @ORM\Column(type="string") */ public $id; /** * @ORM\Column(type="string") */ public $name; } class FooRepository extends BaseRepository { public function findIdByName(string $customName): ?string { $qb = $this->createQueryBuilder('foo') ->select('foo.id') ->andWhere('foo.name = :name') ->setParameter('name', $customName) ->setMaxResults(1); try { return $qb->getQuery()->getSingleScalarResult(); } catch (NoResultException $exception) { // this try/catch should not be needed at all return null; } } } ``` #### Expected behavior Calling getSingleScalarResult() on a query that returns an empty result set returns NULL. PR: https://github.com/doctrine/orm/pull/7895
admin added the BugMissing Tests labels 2026-01-22 15:31:25 +01:00
Author
Owner

@Ocramius commented on GitHub (Nov 14, 2019):

Is this a regression introduced by 2.6.3?

@Ocramius commented on GitHub (Nov 14, 2019): Is this a regression introduced by 2.6.3?
Author
Owner

@Piskvor commented on GitHub (Nov 14, 2019):

Don't know - encountered this on 2.6.3, I will check. From a cursory glance, it seems that it's been for a while with executeUsingQueryCache(), and that it only affects a cached result: in that case, it doesn't seem to setHydrationMode().

@Piskvor commented on GitHub (Nov 14, 2019): Don't know - encountered this on 2.6.3, I will check. From a cursory glance, it seems that it's been for a while with `executeUsingQueryCache()`, and that it only affects a cached result: in that case, it doesn't seem to `setHydrationMode()`.
Author
Owner

@webbiedave commented on GitHub (Apr 18, 2022):

The doc comment specifies that the exception will be thrown and projects over the years may be coded to depend on it:

     /**
     * Gets the single scalar result of the query.
     *
     * Alias for getSingleResult(HYDRATE_SINGLE_SCALAR).
     *
     * @return mixed The scalar result.
     *
     * @throws NoResultException        If the query returned no result.
     * @throws NonUniqueResultException If the query result is not unique.
     */
    public function getSingleScalarResult()
    {
        return $this->getSingleResult(self::HYDRATE_SINGLE_SCALAR);
    }

At this point, if disabling the exception is desired, it may be best to allow that via function parameter or configuration.

@webbiedave commented on GitHub (Apr 18, 2022): The [doc comment](https://github.com/doctrine/orm/blob/2.11.x/lib/Doctrine/ORM/AbstractQuery.php#L985) specifies that the exception will be thrown and projects over the years may be coded to depend on it: ```php /** * Gets the single scalar result of the query. * * Alias for getSingleResult(HYDRATE_SINGLE_SCALAR). * * @return mixed The scalar result. * * @throws NoResultException If the query returned no result. * @throws NonUniqueResultException If the query result is not unique. */ public function getSingleScalarResult() { return $this->getSingleResult(self::HYDRATE_SINGLE_SCALAR); } ``` At this point, if disabling the exception is desired, it may be best to allow that via function parameter or configuration.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6342