加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 如何在Doctrine2中限制关联的实体结果?

发布时间:2020-12-13 13:36:12 所属栏目:PHP教程 来源:网络整理
导读:我有以下查询: $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 u,p,m
                      FROM MyCoreBundle:User u
                      JOIN u.programmes p
                      JOIN u.motivation m
                      ');

$result = $query->getResult();

我想限制为每个用户返回的动机对象是我在其他地方使用的第二个查询的结果(在Motivation存储库中):

$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();

有没有办法在第一个查询或更好的方法中限制和限制动机?

您不能限制关节行的数量.

如果你有Doctrine 2.1,你可以在集合上使用 – > slice():

$collection = $user->getMotivations();    // returns a LazyCollection,// makes no SQL query

$motivations = $collection->slice(0,20); // queries the first 20 motivations 
                                          // for this user (if the association
                                          // was not fetch-joint)

见http://www.doctrine-project.org/docs/orm/2.0/en/tutorials/extra-lazy-associations.html

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读