原则-OneToMany关系,所有结果行均未在对象中提取 [英] Doctrine - OneToMany relation, all result row doesn't fetch in object
问题描述
我尝试将所有对象DemandCab与其子对象(DecisionCab)一起获取.
I try to get all my objects DemandCab with their children object (DecisionCab).
我的2个实体
/**
* DemandCab.
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="DemandCabRepository")
*/
class DemandCab
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var DecisionCab
*
* @ORM\OneToMany(targetEntity="\My\CabBundle\Entity\DecisionCab", mappedBy="demandCab")
*/
private $decisionsCab;
/**
* @var \DateTime
*
* @ORM\Column(name="startDate", type="datetime")
*/
private $startDate;
/**
* @var \DateTime
*
* @ORM\Column(name="endDate", type="datetime", nullable=true)
*/
private $endDate;
/**
* @var int
*
* @ORM\Column(name="followup", type="integer", nullable=true)
*/
private $followup;
/**
* @var InfoCab
*
* @ORM\ManyToOne(targetEntity="\My\CabBundle\Entity\InfoCab", inversedBy="demandsCab")
*/
private $infoCab;
}
/**
* DecisionCab.
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="DecisionCabRepository")
*/
class DecisionCab
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var DemandCab
*
* @ORM\ManyToOne(targetEntity="\My\CabBundle\Entity\DemandCab", inversedBy="decisionsCab")
*/
private $demandCab;
/**
* @var bool
*
* @ORM\Column(name="decision", type="boolean", nullable=true)
*/
private $decision;
/**
* @var string
*
* @ORM\Column(name="motif", type="string", length=500, nullable=true)
*/
private $motif;
/**
* @var string
*
* @ORM\Column(name="role", type="string", length=255)
*/
private $role;
/**
* @var \DateTime
*
* @ORM\Column(name="date", type="datetime", nullable=true)
*/
private $date;
/**
* @var DemandCab
*
* @ORM\ManyToOne(targetEntity="\My\CabBundle\Entity\DemandCab", inversedBy="decisionsCab")
*/
private $demandCab;
/**
* @var User
*
* @ORM\ManyToOne(targetEntity="\My\CabBundle\Entity\User", inversedBy="decisionsCab")
*/
private $user;
}
在我的DemandCab存储库中
In my DemandCabRepository
public function findAllCompleted(){
$qb = $this->createQueryBuilder("dem");
$qb->select('dem, dec');
$qb->leftJoin("dem.decisionsCab", "dec");
$qb->andWhere("dem.completed = 1");
$qb->orderBy("dem.startDate", "DESC");
return $qb->getQuery()->getResult();
}
我的DemandCab数据
My DemandCab data
我的DecisionCab数据
My DecisionCab data
当我转储结果时,仅出现2个决定...
When i dump result, only 2 decisions appear ...
...而当我使用getArrayResult
时,我有4个决定...
... whereas when i use getArrayResult
, i have my 4 decisions ...
查询很好,但我不明白为什么水化会删除属性decision
为0或1(仅显示null)的DecisionCab对象.
The query is good but i dont understand why hydration remove DecisionCab object with attribute decision
at 0 or 1 (only null are display).
我想了解为什么以及是否有一种解决方案,可以将DemandCab对象与所有DecisionCab子对象一起获得.
I would like to understand why and is there a solution to get DemandCab object with all DecisionCab children object.
谢谢
推荐答案
我能够重现您的问题,但是我不确定这是否是您的情况.
I am able to reproduce your issue, but I am not sure if this is your case.
无论如何,我的假设是,在查询生成器的帮助下,您至少查询一次具有 decision 关系的需求实体.也许是在您的操作,事件侦听器或代码中的其他地方完成的.
Anyway, my assumption is that you query the demand Entity joined with decision relation at least once with the help of a query builder. Maybe this is done in your action, in an event listener or somewhere else in your code.
所以您可能会遇到类似的事情:
So you may have something like:
$qb = $this->getDoctrine()
->getRepository(DemandCab::class)->createQueryBuilder("dem");
$qb->select('dem, dec');
$qb->leftJoin("dem.decisionsCab", "dec");
$qb->andWhere("dec.decision IS NULL");
$qb->orderBy("dem.startDate", "DESC");
$results = $qb->getQuery()->getResult(); // <-- the decisionsCab collection is hydrated but filtered
$qb2 = $this->getDoctrine()
->getRepository(DemandCab::class)->createQueryBuilder("dem");
$qb2->select('dem, dec');
$qb2->leftJoin("dem.decisionsCab", "dec");
$qb2->andWhere("dem.completed = 1");
$qb2->orderBy("dem.startDate", "DESC");
$q = $qb2->getQuery();
//$q->setHint(Query::HINT_REFRESH, true);
$results = $q->getResult();
问题出在 Doctrine \ ORM \ Internal \ Hydration \ ObjectHydrator 中,它的属性为" initializedCollections ",其中保留了已初始化的集合,并对这些集合进行了散列父实体类型和实体本身.不幸的是,在上述情况下,混合器无法理解在第一个查询中对集合进行了过滤,并在第二个查询中使用了该集合以避免补水.(
The issue is in Doctrine\ORM\Internal\Hydration\ObjectHydrator, it has the property "initializedCollections" where already initialized collections are kept and the collections are hashed by the parent entity type and the entity itself. Unfortunately in the above case, the heydrator does not understand that the collection is filtered in the 1st query and uses it in the 2nd query in order to avoid rehydration.(github link)
解决方案是使查询生成器刷新.尝试输入代码:
The solution is to cause the query builder to refresh. Try the code:
$qb->orderBy("dem.startDate", "DESC");
$q = $qb->getQuery();
$q->setHint(Query::HINT_REFRESH, true); // <-- Tell the hydrator to refresh
return $q->getResult();
这篇关于原则-OneToMany关系,所有结果行均未在对象中提取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!