错误:无效的PathExpression。必须是StateFieldPathExpression在字段选择上失败

时间:2018-06-13 09:02:10

标签: php mysql doctrine

我使用createQueryBuilder创建这样的查询

$result = $qb->select('csr.id,csr.survey')
              ->from('Entity\ClientSurveyRecord', 'csr')
              ->innerJoin('Entity\AbstractClientRecord','cr','WITH','cr.id = csr.id')
              ->innerJoin('Entity\Client','c','WITH','cr.client = c.id')
              ->where('csr.survey = :id_survey')
              ->setParameter('id_survey',$id)
              ->getQuery()
              ->getResult();

我收到以下消息类型:Doctrine \ ORM \ Query \ QueryException

Message: [Semantical Error] line 0, col 18 near 'survey FROM Entity\ClientSurveyRecord': Error: Invalid PathExpression. Must be a StateFieldPathExpression.

Filename: /var/www/vendor/doctrine/orm/lib/Doctrine/ORM/Query/QueryException.php

但如果我为$qb->select('csr.id,csr.survey')更改了$qb->select('csr.id'),那么

这是映射文件

Entity\ClientSurveyRecord:
    type: entity
    table: clients_survey_records

    fields:

        result:
            type: integer
            column: result
            nullable: false
            options:
                comment: Client survey current result.

    manyToOne:
        survey:
            targetEntity: Entity\AbstractSurvey
            joinColumn:
                name: id_survey
                referenceColumnName: id
                nullable: false

        surveyShipmentTracking:
            targetEntity: Entity\SurveyShipmentTracking
            joinColumn:
                name: id_survey_shipment_tracking
                referenceColumnName: id
                nullable: false

1 个答案:

答案 0 :(得分:0)

您需要使用映射属性加入关系,例如需要在查询构建器对象中加入此关联的调查

$result = $qb->select(['csr.id','s']) // or add column names ['csr.id','s.id','s.title', ...]
              ->from('Entity\ClientSurveyRecord', 'csr')
              ->innerJoin('csr.survey','s')
              ->innerJoin('Entity\AbstractClientRecord','cr','WITH','cr.id = csr.id')
              ->innerJoin('Entity\Client','c','WITH','cr.client = c.id')
              ->where('s.id = :id_survey')
              ->setParameter('id_survey',$id)
              ->getQuery()
              ->getResult();

如果您使用一些映射属性加入Entity\AbstractClientRecordEntity\Client也是一件好事,就像您已经为调查所做的那样,例如

$result = $qb->select(['csr.id','s'])
              ->from('Entity\ClientSurveyRecord', 'csr')
              ->innerJoin('csr.survey','s')
              ->innerJoin('csr.abstractClientRecord','cr')
              ->innerJoin('cr.client','c')
              ->where('s.id = :id_survey')
              ->setParameter('id_survey',$id)
              ->getQuery()
              ->getResult();