如何限制 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();
有没有办法在第一个查询中限制和限制动机或者更好的方法?
I have the following query:
$query = $this->getEntityManager()->createQuery('
SELECT u, p, m
FROM MyCoreBundle:User u
JOIN u.programmes p
JOIN u.motivation m
');
$result = $query->getResult();
I want to restrict the motivation objects returned for each user to be the result of this second query which I am using elsewhere (On the Motivation repository):
$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();
Is there a way to limit and restrict motivation in the first query or a better approach?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法限制联合行的数量。
如果您有 Doctrine 2.1,则可以在集合上使用
->slice()
:请参阅 http://www.doctrine-project.org/docs/orm/2.0/en/tutorials/extra-lazy-associations.html
You can't limit the number of joint rows.
If you have Doctrine 2.1 you can use
->slice()
on the collection:See http://www.doctrine-project.org/docs/orm/2.0/en/tutorials/extra-lazy-associations.html