如何获得多对多关系中没有与DQL和Doctrine对应的链接实体的实体? [英] How to get entities in a many-to-many relationship that do NOT have a corresponding linked entity with DQL and Doctrine?

查看:78
本文介绍了如何获得多对多关系中没有与DQL和Doctrine对应的链接实体的实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经建立了标准的多对多关系.实体A可以具有许多实体B,反之亦然.

I have a standard many-to-many relationship set up. Entity A can have many of Entity B, and vice versa.

我试图获取没有任何对应实体B的所有实体A的列表.在SQL中,我将运行如下查询:

I'm trying to get a list of all Entity A that do NOT have any corresponding Entity B. In SQL, I'd run a query like this:

SELECT a.* FROM entity_a a LEFT JOIN a_b r ON r.AID = a.id WHERE r.BID IS NULL

在此查询中,a_b是链接表.

In this query, a_b is the linking table.

我正在尝试编写DQL语句(或使用其他方法)来获得相同的结果,但是以下操作无效:

I'm trying to write a DQL statement (or use some other method) to get the same result, but the following does not work:

SELECT s FROM VendorMyBundle:EntityA s LEFT JOIN VendorMyOtherBundle:EntityB u WHERE u IS NULL

我如何实现我想要做的事情?

How can I achieve what I'm trying to do?

推荐答案

首先,我必须强调,通常您应该加入实体的属性(即 s ),例如代替:

First, I have to underline that usually you should JOIN on the property of the entity (i.e. s), e.g. instead of:

SELECT s FROM VendorMyBundle:EntityA s 
  LEFT JOIN VendorMyOtherBundle:EntityB u WHERE u IS NULL

您应该具有以下内容:

SELECT s FROM VendorMyBundle:EntityA s 
  LEFT JOIN s.mylistofb u WHERE u IS NULL

我假设在实体A中,您已将您的关系定义为:

where I'm supposing that in entity A you have defined your relationship as:

class A{
    // ...

    /**
     * @ManyToMany(targetEntity="Vendor\MyBundle\Entity\EntityB")
     * @JoinTable(name="as_bs",
     *      joinColumns={@JoinColumn(name="a_id", referencedColumnName="id")},
     *      inverseJoinColumns={@JoinColumn(name="b_id", referencedColumnName="id", unique=true)}
     *      )
     **/
    private $mylistofb;

这表明,如果查询仍无法进行,请尝试以下操作:

This stated, if the query is not working yet, then try the following:

SELECT s FROM VendorMyBundle:EntityA s
  WHERE SIZE(s.mylistofb) < 1

它比以前最简单,并且来自

It is simplest than the previous and also comes from the official docs (i.e. see "phonenumbers example").

这篇关于如何获得多对多关系中没有与DQL和Doctrine对应的链接实体的实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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