Doctrine2: [Semantical Error] Cannot select entity through identification variables without choosing at least one root entity alias

谁说胖子不能爱 提交于 2019-12-06 03:40:30

The problem with your second query is that you are trying to return the associated modules objects but not the User object. Doctrine doesn't like this, and doesn't operate this way. I know you tried to hydrate an array, but if you hadn't, this is what your first query would be trying to return:

User object {
    ...,
    $modules -> array of Module objects
}

So you'd return a single User object, and then your $modules member of that User class is going to be an array of all associated module objects. Even though you are selecting u, m, Doctrine still wants to return that one object because m is just an association on u. Just because you want to hydrate an array doesn't change how Doctrine wants to select your data to begin with.

Your second example - Doctrine doesn't know how to return that. By individually selecting User fields, you are no longer returning a User object but an array of User values. So, where could it even return associated modules there? It wouldn't make sense to return this format:

[
    user id,
    user name,
    user email,
    [ array of Module objects ]
]

This is even trickier as a Many-to-Many association. The real question is, what are you trying to accomplish with your second query that you find unacceptable with your first query? If it's performance, you're probably not gaining much with the second query. If it's simply returning specific columns, then you should be specifying those m. columns as well.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!