Playing around with Single Result on Query Builder (doctrine)

I was always wondering what happens with the "undefined" or "unexpected" errors that may occur due to a number of reasons from bad coding habbits to inability to predict all possible inputs. Maybe this is the main reason a "BETA" sign keeps appearing on top of many online services, even famous ones. In everyday life with Symfony what appears as an "exception" in "production" mode is usually BLANK page... or "the white screen of death" (yet development environment is a savior)!

After some years of coding I realized that there is an invention called EXCEPTION and a simple block of code containing the words try and catch. Oh YES, coding enables you to do mistakes by introducing something as simple as: try to do something and if it fails try to do something else.

Here comes today's little playing around with doctrine, query builder and getSingleResult function. Why to use this function and not follow the "easy" and "bloodless" path of get Results. The explanation is pretty simple. All the functions return arrays which need special treatment, while SingleResult or findOne return OBJECTS, and we can use it when we know that we need to fetch ONE object.

The usual usage of the function is something like the following:
$repository = $this->getDoctrine()
                ->getRepository('MyPreciousBundle:Human');
$query = $repository->createQueryBuilder('h')
                ->where('h.name = :somename')
                ->setParameter('somename', '
Odysseus')

                ->getQuery();
$result = $query->getSingleResult();
But happens when there is no Single Result as we expect it to be? Easy answer... try it and if it fails catch the exception. If you try to try:
try {
    $
result = $query->getSingleResult();
} catch (Exception $e) {
    return null;
}

...you will not eventually have the desired result. The reason is somewhat simple but not obvious. The exception in this case is not a PHP error, but a doctrine error. This means that the simple and generic "Exception $e" by PHP won't do the job. What is needed as extra is the exact Doctrine exception expected to occur which is the noresultexception or nonuniqueresultexception (according to getSingleResult spec: Github doctrine). So, in order to fully implement the correct try/catch we need to:
try {    $result = $query->getSingleResult();
} catch (\Doctrine\ORM\NoResultException $e) {
    return null;
} catch (\Doctrine\ORM\NonUniqueResultException $e) {
    return null;
} catch (Exception $e) {
    return null;
}
In this case we can catch the Exceptions and treat each one of them the way we like. Another function also exists that limit the exceptions to one less. The function is getOneOrNullResult, which returns only one or null if no results occur. Again, the NonUniqueResultException has to be caught exclusively.

Coding is all about joy and creativity, but we need to be careful in order not to create deadlocks to the users of our services.

So code carefully and ...ENJOY!

Comments

  1. Casinos Near Casinos Near Casino Slots, Atlantic City
    Hotels near casinos · Casinos Near Casinos Near 광주 출장안마 Casino Slots, Atlantic City 남양주 출장샵 · Harrah's Cherokee 여수 출장안마 Casino & Hotel 충청남도 출장샵 · Caesars Atlantic City Casino 논산 출장마사지 & Spa

    ReplyDelete

Post a Comment

Popular Posts