Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

symfony - "Invalid PathExpression. Must be a StateFieldPathExpression" in query builder with non related entities

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('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:

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...