getOneOrNullResult contains duplicate code in Doctrine\ORM\AbstractQuery #5303

Open
opened 2026-01-22 15:03:59 +01:00 by admin · 1 comment
Owner

Originally created by @naeh on GitHub (Oct 25, 2016).

Hello,

There is duplicate code in getOneOrNullResult (code from getSingleResult), i think this function should be like this :

public function getOneOrNullResult($hydrationMode = null)
{
    try {
        return $this->getSingleResult($hydrationMode);
    } catch (NoResultException $e) {
        return null;
    }
}

am i right ?

Originally created by @naeh on GitHub (Oct 25, 2016). Hello, There is duplicate code in **getOneOrNullResult** (code from **getSingleResult**), i think this function should be like this : ``` public function getOneOrNullResult($hydrationMode = null) { try { return $this->getSingleResult($hydrationMode); } catch (NoResultException $e) { return null; } } ``` am i right ?
admin added the Improvement label 2026-01-22 15:03:59 +01:00
Author
Owner

@Ocramius commented on GitHub (Oct 25, 2016):

You mean Doctrine\ORM\AbstractQuery#getOneOrNullResult() ?

Feel free to send a patch, but the solution is not to use getSingleResult internally (since somebody may override it), but instead use a private method in both public methods.

@Ocramius commented on GitHub (Oct 25, 2016): You mean [`Doctrine\ORM\AbstractQuery#getOneOrNullResult()`](https://github.com/doctrine/doctrine2/blob/26cf90e1c513305e1cc6529e4496130a7f0c6dcf/lib/Doctrine/ORM/AbstractQuery.php#L762) ? Feel free to send a patch, but the solution is not to use `getSingleResult` internally (since somebody may override it), but instead use a private method in both public methods.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5303