Doctrine 2 Native Query select from joined entity
Asked Answered
T

1

11

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?

Throve answered 13/11, 2014 at 14:30 Comment(3)
Why would you want to use a NativeQuery (in stead of DQL) for this case?Disciplinary
I was bored and just don't want to limit myself :PThrove
Actually you are limiting yourself because with NativeQuery you're using SQL which is vendor specific, while DQL is vendor agnostic ;) But for the sake of testing and getting to know the library I'm all with you!Disciplinary
T
16

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

Throve answered 15/11, 2014 at 20:50 Comment(1)
You indeed need to fetch the identifiers used in the association between CrmBusinessPartner and RefCountry, otherwise Doctrine cannot determine which belong to which.Disciplinary

© 2022 - 2024 — McMap. All rights reserved.