【发布时间】:2012-08-06 08:43:10
【问题描述】:
我想获取最后一个用户配置文件。但我无法在 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() 那么它说结果不是唯一的
【问题讨论】:
-
您尝试过更改
setMaxResult(1)吗? -
我也尝试过 result(1) ,但这也返回了数组而不是单个对象
-
setMaxResults(1) 工作正常...