gpt4 book ai didi

symfony - 原则 2 native 查询从连接实体中选择

转载 作者:行者123 更新时间:2023-12-02 09:01:48 25 4
gpt4 key购买 nike

注意:这是 native 查询特定的

我有 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:

CrmBusinessPartnerRepository”存储库使用 ResultSetMappingBuilder 而不是 ResultSetMapping,这是一个工作代码:

$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

我想知道为什么/如何使用 native 查询(不)工作/执行(它)

最佳答案

好吧,好吧,好吧......看来您必须在查询中添加 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();

它正在按预期工作。

我希望有人之前告诉过我这一点(教义文档...看着你):D

关于symfony - 原则 2 native 查询从连接实体中选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26911204/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com