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?