Doctrine 2 Native Query select from joined entity

前端 未结 1 1261
旧时难觅i
旧时难觅i 2021-02-20 05:08

NOTE: This is Native Query specific

I have 2 related entites \"CrmBusinessPartner\" and \"RefCountry<

1条回答
  •  名媛妹妹
    2021-02-20 05:41

    Well, well, well...it looks like you have to add rc.id AS ref_country_id, in query and $rsm->addFieldResult('rc','ref_country_id','id'); like this:

    $rsm = new \Doctrine\ORM\Query\ResultSetMapping();
    
    $rsm->addEntityResult('Entity\CrmBusinessPartner', 'bp');
    
    $rsm->addFieldResult('bp','id','id');
    $rsm->addFieldResult('bp','vat','vat');
    $rsm->addFieldResult('bp','business_partner_name','name');
    
    $rsm->addJoinedEntityResult('Entity\RefCountry', 'rc', 'bp', 'billingCountry');
    $rsm->addFieldResult('rc','ref_country_id','id');
    $rsm->addFieldResult('rc','ref_country_name','name');
    
    $em = $this->getEntityManager()
        ->createNativeQuery('
            SELECT
              bp.id,
              bp.vat,
              bp.name AS business_partner_name,
              rc.id AS ref_country_id,
              rc.name AS ref_country_name
            FROM crm_business_partner bp
            LEFT JOIN ref_country rc ON bp.ref_country_id=rc.id
            ',
            $rsm
        );
    
    return $em->getResult();
    

    It is working as expected like this.

    I wish somebody told me this before (Doctrine documentation...watching you) :D

    0 讨论(0)
提交回复
热议问题