“Invalid PathExpression. Must be a StateFieldPathE

2019-01-18 03:18发布

问题:

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 

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();
}

But any time I call the function from my controller I get this error:

[Semantical Error] line 0, col 10 near 'media, od.type,': Error: Invalid PathExpression. Must be a StateFieldPathExpression.

[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 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: I'm using latest Symfony2 and Doctrine2 if you need to take a look to my entities here they are: OfficialDocument, Media, NMediaStatus, NMediaType

回答1:

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');

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



回答2:

Simply you can use () where composite keys exist.

$qb->select('(od.media)', '(od.type) AS doc_type', '(od.status) AS doc_status', 'md.url', 'nms.name', 'nmt.name');