【问题标题】:Doctrine2: Many-to-Many select all entries, with/without relationsDoctrine2:多对多选择所有条目,有/没有关系
【发布时间】:2018-06-18 11:41:16
【问题描述】:

我的应用中存在多对多双向关系,就像在 official guide 中一样。

使用上面链接中的关系模型User Group - 假设我想从具有以下条件的Group 实体中选择所有条目

1) Select all groups that have at least one user related to a group.
2) Select all groups that have no users in it.

我不知道如何准备正确的 DQL,请有任何想法。

【问题讨论】:

    标签: doctrine-orm many-to-many


    【解决方案1】:

    在纯 DQL 中你可以写成

    SELECT g, 
           COUNT(u.id) AS total_users
    FROM Entity\Group g
    LEFT JOIN g.users u
    GROUP BY g.id
    HAVING total_users >= 0
    

    Doctrine2 get object without relations

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-19
      • 2023-03-23
      • 1970-01-01
      相关资源
      最近更新 更多