我有以下查询:如何限制Doctrine2中的关联实体结果?
$query = $this->getEntityManager()->createQuery('
SELECT u, p, m
FROM MyCoreBundle:User u
JOIN u.programmes p
JOIN u.motivation m
');
$result = $query->getResult();
我要限制的动机返回的对象为每个用户是哪些,我在其他地方使用(在激励库)本次查询的结果:
$query = $this->getEntityManager()->createQuery('
SELECT m FROM MyCoreBundle:Motivation m
WHERE m.user = :user
ORDER BY m.date DESC');
$query->setParameter('user',$user);
$query->setFirstResult(0);
$query->setMaxResults(1);
//@TODO if there is not result recorded for the user, return sth which indicates this
return $query->getResult();
有没有办法在第一个查询或更好的方法中限制和限制动机?
考虑写在下面,并假设“最新motiviation”是最重要的对你,这将是访问了很多,可能的解决方案是创建'User:LatestMotivation' OneToOne relatinship。然后每次添加一个新的Motivation实体时,(通过Doctrine'prePersist'事件)用新添加的一个更新'LatestMotivation'。通过这种方式,您将能够遍历许多获取最新动机的“用户”记录。 HTH –