Doctrine 2 Native Query从已连接的实体中选择

时间:2014-11-13 14:30:54

标签: symfony doctrine-orm symfony-2.5

注意: 这是原生查询特定

我有2个相关网站“ CrmBusinessPartner ”和“ RefCountry ”和“ CrmBusinessPartnerRepository ”存储库。

CrmBusinessPartner 实体

/**
 * 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 实体

/**
 * 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();
    }
}

在“ CrmBusinessPartnerRepository ”存储库中有以下工作代码:

$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();

数据库同步(根据教义:架构:更新):

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

这是结果的转储:

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

问题:

我无法理解为什么对于所有参与者来说,billingCountries总是为空?


修改

我已经使用QueryBuilder实现了解决方案:

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

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

编辑2:

使用 ResultSetMappingBuilder 而不是 ResultSetMapping

CrmBusinessPartnerRepository ”存储库,这是一个正常工作的代码:

$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();

结果相同 billingCountry 对所有实体都是空的:

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

我想知道使用原生查询为什么/如何(不)工作/做(?)?

1 个答案:

答案 0 :(得分:15)

嗯,好吧,好吧......看起来您必须在查询中添加rc.id AS ref_country_id,,并$rsm->addFieldResult('rc','ref_country_id','id');这样:

$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();

正如预期的那样工作。

我希望有人告诉过我(Doctrine文档......看着你):D