How can i get the single result using DQL in symfony2 How can i get the single result using DQL in symfony2 symfony symfony

How can i get the single result using DQL in symfony2


The right method is:

$singleProfile = $em->createQuery($dql)                    ->setParameter('user_id',$user_id)                    ->getSingleResult();

To prevent error then no results try this:

$singleProfile = $em->createQuery($dql)                    ->setParameter('user_id',$user_id)                    ->getOneOrNullResult();


                $allProfiles = $em->createQuery($dql)                                ->setParameter('user_id',$user_id)                                ->setMaxResults(1)                                ->getResult();                return $allProfiles[0];