问题描述
我有四个实体:OfficialDocument
、Media
、NMediaStatus
和 NMediaType
.我正在尝试翻译此 SQL:
I have four entities: OfficialDocument
, Media
, NMediaStatus
and NMediaType
. I'm trying to translate this SQL:
SELECT od.media, od.type, od.status, md.url, nms.name
FROM official_document od
LEFT JOIN media md ON od.media = md.id
LEFT JOIN n_media_status nms ON od.status = nms.id
WHERE od.company = 9
到Doctrine Query Builder,结果如下:
to Doctrine Query Builder and this is the result:
public function findOfficialDocument($company_id) {
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->select('od.media', 'od.type', 'od.status', 'md.url', 'nms.name', 'nmt.name');
$qb->from('CompanyRegisterCompanyBundleEntityOfficialDocument', 'od');
$qb->leftJoin('CommonMediaBundleEntityMedia', 'md', DoctrineORMQueryExprJoin::WITH, 'od.media = md.id');
$qb->leftJoin('CommonMediaBundleEntityNMediaStatus', 'nms', DoctrineORMQueryExprJoin::WITH, 'od.status = nms.id');
$qb->leftJoin('CommonMediaBundleEntityNMediaType', 'nmt', DoctrineORMQueryExprJoin::WITH, 'od.type = nmt.id');
$qb->where('od.company = ?1');
$qb->setParameter(1, $company_id);
return $qb->getQuery()->getResult();
}
但是每当我从控制器调用该函数时,我都会收到此错误:
But any time I call the function from my controller I get this error:
[语义错误] 'media, od.type,' 附近的第 0 行第 10 列:错误:无效的路径表达式.必须是 StateFieldPathExpression.
[1/2] QueryException: SELECT od.media, od.type, od.status, md.url,nms.name, nmt.name FROMCompanyRegisterCompanyBundleEntityOfficialDocument od LEFT JOINCommonMediaBundleEntityMedia md WITH od.media = md.id LEFT JOINCommonMediaBundleEntityNMediaStatus nms WITH od.status = nms.id左连接 CommonMediaBundleEntityNMediaType nmt WITH od.type =nmt.id WHERE od.company = ?1
[1/2] QueryException: SELECT od.media, od.type, od.status, md.url, nms.name, nmt.name FROM CompanyRegisterCompanyBundleEntityOfficialDocument od LEFT JOIN CommonMediaBundleEntityMedia md WITH od.media = md.id LEFT JOIN CommonMediaBundleEntityNMediaStatus nms WITH od.status = nms.id LEFT JOIN CommonMediaBundleEntityNMediaType nmt WITH od.type = nmt.id WHERE od.company = ?1
OfficialDocument
与其他三个实体相关,但由于我不需要这些实体中的 reversedBy
,所以我认为这是导致错误的原因,或者可能不是,不确定.无论如何,有什么建议或帮助来解决这个问题吗?
OfficialDocument
is related to the other three entities, but since I don't need the reversedBy
in those entities then I tough this is causing the error, or maybe not, not sure about it. Anyway, any advice or help to fix this issue?
PS:我正在使用最新的 Symfony2 和 Doctrine2,如果您需要查看我的实体,它们是:官方文档、媒体、NMediaStatus, NMediaType
PS: I'm using latest Symfony2 and Doctrine2 if you need to take a look to my entities here they are: OfficialDocument, Media, NMediaStatus, NMediaType
推荐答案
我自己回答,因为我找到了解决方法:
I answer myself since I found how to fix it:
$qb->select('IDENTITY(od.media)', 'IDENTITY(od.type) AS doc_type', 'IDENTITY(od.status) AS doc_status', 'md.url', 'nms.name', 'nmt.name');
由于 od.media
、od.type
、od.status
是复合键,所以我需要添加 IDENTITY代码>以使查询工作并解决问题
Since od.media
, od.type
, od.status
are composite keys then I need to add IDENTITY
in order to make the query work and fix the issue
这篇关于"无效的路径表达式.必须是 StateFieldPathExpression"在具有非相关实体的查询构建器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!