Symfony2和Doctrine:使用查询构建器的多对多关系 [英] Symfony2 and Doctrine: Many-to-many relation using query builder

查看:229
本文介绍了Symfony2和Doctrine:使用查询构建器的多对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个实体:用户和存档。用户实体有两个属性:

  / ** 
* @ ORM\OneToMany(targetEntity =My\ApplicationBundle\Entity\Archive,mappedBy =user)
** /
protected $ archives;

/ **
* @ ORM\ManyToMany(targetEntity =My\ApplicationBundle\Entity\Archive,inversedBy =users)
* @ORM \JoinTable(name =collection)
** /
private $ collection;

和存档实体:

  / ** 
* @ ORM\ManyToOne(targetEntity =My\UserBundle\Entity\User,inversedBy =archives)
* @ORM \JoinColumn(name =user_id,referencedColumnName =id)
** /
protected $ user;

/ **
* @ ORM\ManyToMany(targetEntity =My\UserBundle\Entity\User,mappedBy =collection)
** /
private $ users;

这个小小的麻烦的原因很简单:在User中,$ archives代表所有归档文件$ collection表示用户使用的所有档案(甚至由其他用户提交)。在存档实体中,$ user表示提交此归档的用户,$ users表示使用此归档的所有用户。



所以,为了简单地获取一个用户的归档用于做:

  $ this-> getUser() - > getArchives(); 

并收集:

  $这 - >的getUser() - > getCollection(); 

现在的问题是我必须分页结果,所以我需要使用查询构建器。我正在做的是:

  $ repository = $ this-> getDoctrine()
- > getRepository 'MyApplicationBundle:存档');

$ query = $ repository-> createQueryBuilder('a')
- > innerJoin('a.user','u','WITH','u =:user ')
- > where('a.active =:active')
- > setParameters(array(
'user'=> $ this-> getUser()
'active'=> 1
))
- > setFirstResult($ start)
- > setMaxResults($ length)
- > getQuery )
- > getResult();

但这里的问题是我直接去Archive而不使用收集表(没有实体)。任何人都可以解释一下我的热点,以考虑收集表过滤我的结果?



非常感谢
Manuel



感谢LPodolski,这是我如何更改DQB


$($)

SOLVED



b $ b

  $ query = $ repository-> createQueryBuilder('a')
//->innerJoin('a.user','u' 'WITH','u =:user')
- > innerJoin('a.users','cu','WITH','cu =:user')
- > where 'a.active =:active')
- > setParameters(array(
'user'=> $ this-> getUser(),
'active'=> 1

- > setFirstResult($ start)
- > setMaxResults($ length)
- > getQuery()
;


解决方案

 > innerJoin('a.users','cu','WITH','cu =:collectionUser')

或leftJoin应该做的技巧


I have 2 entities: User and Archive. The user entity has, among others, two properties:

/**
 * @ORM\OneToMany(targetEntity="My\ApplicationBundle\Entity\Archive", mappedBy="user")
 **/
protected $archives;

/**
 * @ORM\ManyToMany(targetEntity="My\ApplicationBundle\Entity\Archive", inversedBy="users")
 * @ORM\JoinTable(name="collection")
 **/
private $collection;

and the Archive entity:

/**
 * @ORM\ManyToOne(targetEntity="My\UserBundle\Entity\User", inversedBy="archives")
 * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
 **/
protected $user;

/**
 * @ORM\ManyToMany(targetEntity="My\UserBundle\Entity\User", mappedBy="collection")
 **/
private $users;

the reason of this little mess is pretty simple: in User, $archives represent all the archives submitted by a user, $collection represent all the archives used by a user (even submitted by other users). In Archive entity, $user represent the user that submitted this archive, $users represent all the users using this archive.

So, to get the archives of one user I simply use to do:

$this->getUser()->getArchives();

and to get the collection:

$this->getUser()->getCollection();

the problem now is I have to paginate the results, so I need to use query builder. what I am doing is:

$repository = $this->getDoctrine()
        ->getRepository('MyApplicationBundle:Archive');

    $query = $repository->createQueryBuilder('a')
        ->innerJoin('a.user', 'u', 'WITH', 'u = :user')
        ->where('a.active = :active')
        ->setParameters(array(
            'user' => $this->getUser(),
            'active' => 1
        ))
        ->setFirstResult($start)
        ->setMaxResults($length)
        ->getQuery()
        ->getResult();

but here the problem is I am going directly to Archive without using the collection table (there is not an entity for that). Can anyone please explain me hot to consider the collection table to filter my results?

Many thanks Manuel


SOLVED

thanks to LPodolski, this is how I changed the DQB

$query = $repository->createQueryBuilder('a')
        //->innerJoin('a.user', 'u', 'WITH', 'u = :user')
        ->innerJoin('a.users', 'cu', 'WITH', 'cu = :user')
        ->where('a.active = :active')
        ->setParameters(array(
            'user' => $this->getUser(),
            'active' => 1
        ))
        ->setFirstResult($start)
        ->setMaxResults($length)
        ->getQuery()
;

解决方案

>innerJoin('a.users', 'cu', 'WITH', 'cu = :collectionUser')

or leftJoin should do the trick

这篇关于Symfony2和Doctrine:使用查询构建器的多对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆