问题描述
我有四个实体: OfficialDocument
,媒体
, 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查询生成器,这是结果:
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('Company\RegisterCompanyBundle\Entity\OfficialDocument', 'od');
$qb->leftJoin('Common\MediaBundle\Entity\Media', 'md', \Doctrine\ORM\Query\Expr\Join::WITH, 'od.media = md.id');
$qb->leftJoin('Common\MediaBundle\Entity\NMediaStatus', 'nms', \Doctrine\ORM\Query\Expr\Join::WITH, 'od.status = nms.id');
$qb->leftJoin('Common\MediaBundle\Entity\NMediaType', 'nmt', \Doctrine\ORM\Query\Expr\Join::WITH, 'od.type = nmt.id');
$qb->where('od.company = ?1');
$qb->setParameter(1, $company_id);
return $qb->getQuery()->getResult();
}
但是,任何时候我从我的控制器调用这个函数都会得到这个错误: / p>
But any time I call the function from my controller I get this error:
[1/2] QueryException:SELECT od.media,od.type,od.status,md.url,
nms。 name,nmt.name FROM
Company\RegisterCompanyBundle\Entity\OfficialDocument od LEFT JOIN
Common\MediaBundle\Entity\Media md WITH od.media = md.id LEFT JOIN
Common\MediaBundle\Entity\NMediaStatus nms WITH od.status = nms.id
LEFT JOIN Common\MediaBundle\Entity\NMediaType 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 Company\RegisterCompanyBundle\Entity\OfficialDocument od LEFT JOIN Common\MediaBundle\Entity\Media md WITH od.media = md.id LEFT JOIN Common\MediaBundle\Entity\NMediaStatus nms WITH od.status = nms.id LEFT JOIN Common\MediaBundle\Entity\NMediaType nmt WITH od.type = nmt.id WHERE od.company = ?1
OfficialDocument
与其他三个实体,但是由于我不需要这些实体中的 reverseBy
,所以我很难这会导致错误,或者也可能不知道。无论如何,任何建议或帮助来解决这个问题?
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: ,,
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“在与非相关实体的查询构建器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!