![](/img/trans.png)
[英]createQueryBuilder leftJoin using Doctrine DQL and Symfony2
[英]How can i get the single result using DQL in symfony2
我想获取最后一个用户配置文件。 但我无法在 DQL 中做到这一点。 我有这个代码
$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;
它返回所有配置文件。
如果我使用 getSingleResult() 那么它说结果不是唯一的
正确的方法是:
$singleProfile = $em->createQuery($dql)
->setParameter('user_id',$user_id)
->getSingleResult();
为了防止错误然后没有结果试试这个:
$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];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.