简体   繁体   English

Symfony2和Doctrine:使用查询生成器进行多对多关系

[英]Symfony2 and Doctrine: Many-to-many relation using query builder

I have 2 entities: User and Archive. 我有2个实体:用户和存档。 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). 造成这种混乱的原因非常简单:在用户中,$ archives代表用户提交的所有档案,$ collection代表用户使用的所有档案(甚至是其他用户提交的档案)。 In Archive entity, $user represent the user that submitted this archive, $users represent all the users using this archive. 在“存档”实体中,$ user代表提交此存档的用户,$ users代表使用此存档的所有用户。

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 非常感谢Manuel


SOLVED 解决了

thanks to LPodolski, this is how I changed the DQB 多亏了LPodolski,这就是我更改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')

或leftJoin应该可以解决问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM