【发布时间】:2011-09-01 10:53:22
【问题描述】:
我有以下疑问:
$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();
有没有办法限制和限制第一次查询中的动机或更好的方法?
【问题讨论】:
-
考虑到下面写的,并假设“最新动机”对你来说是最重要的并且会被很多人访问,可能的解决方案是创建
User:LatestMotivationOneToOne 关系。然后每次添加一个新的Motivation实体时,(通过 DoctrineprePersist事件)用新添加的实体更新LatestMotivation。这样,您将能够遍历许多User记录以获取最新动机。 HTH
标签: php doctrine-orm symfony