原则查询不同的相关实体 [英] Doctrine query distinct related entity

查看:88
本文介绍了原则查询不同的相关实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能忽略了一些非常简单的东西,只是一直盯着它,但是我不能让这个DQL查询工作。我收到一个例外:

I'm probably overlooking something very simple and just been staring at it too much, but I can't get this DQL query to work. I get an exception stating:

Cannot select entity through identification variables without choosing at least one root entity alias.

这是我的查询。用户与组有多对一关系。注意这是一个单向关系!这可能对您没有任何意义,但在我们的域逻辑中是有道理的。

Here's my query. User has a many-to-one relation to Group. Note that this is a unidirectional relation! That may make no sense to you, but it makes sense in our domain logic.

SELECT DISTINCT g
FROM Entity\User u
LEFT JOIN u.group g
WHERE u.active = :active


$ b $你可以告诉我我在这里缺少什么?

Can you tell me what I am missing here?

推荐答案

我通过做一个子选择来解决这个问题: / p>

I worked around the problem by doing a subselect:

SELECT g
FROM Entity\Group
WHERE g.id IN (
    SELECT DISTINCT g2.id
    FROM Entity\User u
    LEFT JOIN u.group g2
    WHERE u.active = :active
)

这篇关于原则查询不同的相关实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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