Doctrine2:[语义错误]如果没有选择至少一个根实体别名,则无法通过标识变量选择实体 [英] Doctrine2: [Semantical Error] Cannot select entity through identification variables without choosing at least one root entity alias

查看:261
本文介绍了Doctrine2:[语义错误]如果没有选择至少一个根实体别名,则无法通过标识变量选择实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我使用查询生成器进行的查询,它可以完美运行,将用户表和模块表的所有结果都带到了很多对很多的关联:

This is my query with query builder, and it works perfectly, bringing all the results of user table and the modules table, which has a many to many association:

public function getUser($id){
    $qb = $this->getEm()->createQueryBuilder()
    ->select('u', 'm')
    ->from('Adm\Entity\User', 'u')
    ->join('u.modules', 'm')
    ->where('u.id = ?1')
    ->setParameters(array(1 => $id));
    $result = $qb->getQuery()->getResult(\Doctrine\ORM\AbstractQuery::HYDRATE_ARRAY);
    return $result;
}

但是当我尝试从这样的用户中选择特定字段时:

But when i try to select specific fields from user like this:

public function getUser($id){
    $qb = $this->getEm()->createQueryBuilder()
    ->select('u.id, u.name, u.email', 'm')
    ->from('Adm\Entity\User', 'u')
    ->join('u.modules', 'm')
    ->where('u.id = ?1')
    ->setParameters(array(1 => $id));
    $result = $qb->getQuery()->getResult(\Doctrine\ORM\AbstractQuery::HYDRATE_ARRAY);
    return $result;
}

Doctrine引发错误:

Doctrine throws an error:

[Semantical Error] line 0, col -1 near 'SELECT u.id,': Error: Cannot select entity through identification variables without choosing at least one root entity alias.

我想知道该怎么做,从实体中选择特定字段,而不是全部

I would like to know how to do that, to select specific fields from the entity and not all the fields.

推荐答案

第二个查询的问题是您试图返回关联的 modules 对象,但不包含 User 对象。教义不喜欢这样,也不能这样运作。我知道您尝试对数组进行水合处理,但是如果没有这样做,这就是您的第一个查询将试图返回的内容:

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
}

因此,您将返回一个User对象,然后您的该User类的 $ modules 成员将是所有关联模块对象的数组。即使您选择 u,m ,Doctrine仍要返回该对象,因为 m 只是一个关联 u 。仅仅因为您要对数组进行水合操作并不会改变Doctrine要选择数据的方式。

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.

您的第二个示例-Doctrine不知道如何返回那。通过单独选择用户字段,您将不再返回用户对象,而是一个用户值数组。那么,它甚至可以在哪里返回关联的模块?返回此格式没有任何意义:

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 ]
]

作为多对多协会,这甚至更加棘手。真正的问题是,您打算用第二个查询来完成哪些您发现第一个查询不可接受的工作?如果效果不错,第二个查询可能不会给您带来什么好处。如果只是返回特定的列,那么您还应该指定那些 m。列。

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.

这篇关于Doctrine2:[语义错误]如果没有选择至少一个根实体别名,则无法通过标识变量选择实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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