Doctrine:在不选择至少一个根实体别名的情况下,不能通过标识变量选择实体 [英] Doctrine: Cannot select entity through identification variables without choosing at least one root entity alias

查看:19
本文介绍了Doctrine:在不选择至少一个根实体别名的情况下,不能通过标识变量选择实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在查询构建器中使用以下代码来选择分值的平均值以及该平均值所属的类别实体:

I'm using the following code in the query builder, to select an average of score values, and the category entity to which that average belongs:

$queryBuilder = $this->createQueryBuilder('s')
    ->resetDQLPart('select')
    ->select('AVG(s.score) as score, partial c.{reviewCategoryID} as cat')
    ->setParameter('status', ReviewStatusType::ACCEPTED)
    ->join('s.review', 'r')
    ->join('s.category', 'c')
    ->where('r.campsite = :campsite')
    ->andWhere('r.status = :status')
    ->setParameter('campsite', $campsite)
    ->groupBy('c.reviewCategoryID');

$campsite 是评论所属的实体,而分数属于评论,分数有类别.

$campsite is an entity to which a review belongs, while scores belong to a review, and scores have a category.

但是当我尝试执行此操作时,出现错误

But when I try to execute this, i get the error

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

当我调试并检查根别名时,我看到定义了s",这应该是根实体(Score).

When I debug and I check the root aliases, I see that 's' is defined, which is should be the root entity (Score).

知道哪里出了问题吗?

推荐答案

createQueryBuilder() 只能在从匹配实体的存储库中调用时带参数.如果你不从这个存储库调用它,你应该定义一个 from 方法.

createQueryBuilder() can only take a parameter when it is called from the repository of the matching entity. In case you do not call it from this repository you should define a from method.

->from('YourMappingSpace:Campsite', 's')

将参数传递给 createQueryBuilder() 无论如何都是为了方便.您始终可以手动定义它.该函数如下所示(仅在实体存储库中):

Passing a parameter to createQueryBuilder() is for conveniance anyway. You can always define it manually. The function looks like this (Only inside the entity repository):

public function createQueryBuilder($alias)
{
    return $this->_em->createQueryBuilder()
        ->select($alias)
        ->from($this->_entityName, $alias);
}

这篇关于Doctrine:在不选择至少一个根实体别名的情况下,不能通过标识变量选择实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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