Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i get the single result using DQL in symfony2

I want to get the last user profile . But i am not able to do that in DQL. I have this code

$em = $this->getEntityManager();

$dql = "SELECT p  FROM AcmeBundle:UserProfile p 
        WHERE p.user_id = :user_id 
        ORDER BY p.createdAt DESC ";

$allProfiles = $em->createQuery($dql)
                  ->setParameter('user_id', $user_id)
                  ->setMaxResults(5)
                  ->getResult();

return $allProfiles;

It returns all the profiles.

If i use getSingleResult() then it says result not unique

like image 452
user17 Avatar asked Aug 09 '12 06:08

user17


1 Answers

                $allProfiles = $em->createQuery($dql)
                                ->setParameter('user_id',$user_id)
                                ->setMaxResults(1)
                                ->getResult();
                return $allProfiles[0];
like image 120
Carlos Granados Avatar answered Nov 07 '22 19:11

Carlos Granados