Doctrine 2 Native Query select from joined entity

こ雲淡風輕ζ 提交于 2020-01-01 08:38:31

问题


NOTE: This is Native Query specific

I have 2 related entites "CrmBusinessPartner" and "RefCountry" and "CrmBusinessPartnerRepository" repository.

CrmBusinessPartner entity

/**
 * CrmBusinessPartner
 *
 * @ORM\Table(name="crm_business_partner")
 * @ORM\Entity(repositoryClass="CrmBusinessPartnerRepository")
 * @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false)
 * @ExclusionPolicy("none")
 */
class CrmBusinessPartner
{
    /**
     *
     * @var integer
     *
     * @ORM\ManyToOne(targetEntity="RefCountry", inversedBy="businessPartners")
     * @ORM\JoinColumn(name="ref_country_id", referencedColumnName="id")
     */
    private $billingCountry;
}

RefCountry entity

/**
 * RefCountry
 *
 * @ORM\Table(name="ref_country")
 * @ORM\Entity
 */
class RefCountry
{
    /**
     * @ORM\OneToMany(targetEntity="CrmBusinessPartner", mappedBy="billingCountry")
     */
    private $businessPartners;

    public function __construct()
    {
        $this->businessPartners= new ArrayCollection();
    }
}

In "CrmBusinessPartnerRepository" repository there is this working code:

$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_name','name');

$em = $this->getEntityManager()
    ->createNativeQuery('
        SELECT
          bp.id,
          bp.vat,
          bp.name AS business_partner_name,
          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();

Database is in sync (according to doctrine:schema:update):

$ php console doctrine:schema:update
Nothing to update - your database is already in sync with the current entity metadata.

This is dump of results:

array ()
  0 => 
    object(Entity\CrmBusinessPartner)
      private 'id' => int 42
      private 'vat' => string '12345678998' (length=11)
      private 'name' => string 'Name 1' (length=22)
      private 'billingCountry' => null

QUESTION:

I can't see why is billingCountries allways null for all entites?


EDIT:

I already have working solution with QueryBuilder:

$qb = $this->getEntityManager()->createQueryBuilder('bp');
$qb->select(array('bp'));
$qb->from('AppBundle:CrmBusinessPartner', 'bp');
$qb->leftJoin('bp.billingCountry', 'rc');

return $qb->getQuery()->getResult();

EDIT 2:

"CrmBusinessPartnerRepository" repository using ResultSetMappingBuilder instead of ResultSetMapping and this is a working code:

$rsm = new ResultSetMappingBuilder($this->getEntityManager());
$rsm->addRootEntityFromClassMetadata(
    'Entity\CrmBusinessPartner', 
    'bp'
);
$rsm->addJoinedEntityFromClassMetadata(
    'Entity\RefCountry', 
    'rc', 
    'bp', 
    'billingCountry', 
    array(
        'id' => 'billing_country_id', 
        'name'=>'rc_name'
    )
);
$em = $this->getEntityManager()
    ->createNativeQuery('
        SELECT
          bp.id,
          bp.vat,
          bp.name AS business_partner_name,
          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();

Result is same billingCountry is allways null for all entities:

array ()
  0 => 
    object(Entity\CrmBusinessPartner)
      private 'id' => int 42
      private 'vat' => string '12345678998' (length=11)
      private 'name' => string 'Name 1' (length=22)
      private 'billingCountry' => null

I want to know why/how is/to (not) working/do (it) using Native Query?


回答1:


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



来源:https://stackoverflow.com/questions/26911204/doctrine-2-native-query-select-from-joined-entity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!