我尝试获取我的所有对象 DemandCab 及其子对象 (DecisionCab).
I try to get all my objects DemandCab with their children object (DecisionCab).
我的 2 个实体
/**
* DemandCab.
*
* @ORMTable()
* @ORMEntity(repositoryClass="DemandCabRepository")
*/
class DemandCab
{
/**
* @var int
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var DecisionCab
*
* @ORMOneToMany(targetEntity="MyCabBundleEntityDecisionCab", mappedBy="demandCab")
*/
private $decisionsCab;
/**
* @var DateTime
*
* @ORMColumn(name="startDate", type="datetime")
*/
private $startDate;
/**
* @var DateTime
*
* @ORMColumn(name="endDate", type="datetime", nullable=true)
*/
private $endDate;
/**
* @var int
*
* @ORMColumn(name="followup", type="integer", nullable=true)
*/
private $followup;
/**
* @var InfoCab
*
* @ORMManyToOne(targetEntity="MyCabBundleEntityInfoCab", inversedBy="demandsCab")
*/
private $infoCab;
}
/**
* DecisionCab.
*
* @ORMTable()
* @ORMEntity(repositoryClass="DecisionCabRepository")
*/
class DecisionCab
{
/**
* @var int
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var DemandCab
*
* @ORMManyToOne(targetEntity="MyCabBundleEntityDemandCab", inversedBy="decisionsCab")
*/
private $demandCab;
/**
* @var bool
*
* @ORMColumn(name="decision", type="boolean", nullable=true)
*/
private $decision;
/**
* @var string
*
* @ORMColumn(name="motif", type="string", length=500, nullable=true)
*/
private $motif;
/**
* @var string
*
* @ORMColumn(name="role", type="string", length=255)
*/
private $role;
/**
* @var DateTime
*
* @ORMColumn(name="date", type="datetime", nullable=true)
*/
private $date;
/**
* @var DemandCab
*
* @ORMManyToOne(targetEntity="MyCabBundleEntityDemandCab", inversedBy="decisionsCab")
*/
private $demandCab;
/**
* @var User
*
* @ORMManyToOne(targetEntity="MyCabBundleEntityUser", inversedBy="decisionsCab")
*/
private $user;
}
在我的 DemandCabRepository 中
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 的 DecisionCab 对象(仅显示 null).
The query is good but i dont understand why hydration remove DecisionCab object with attribute decision
at 0 or 1 (only null are display).
我想了解为什么以及是否有解决方案来获取包含所有 DecisionCab 子对象的 DemandCab 对象.
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 关系连接的 demand 实体.也许这是在您的操作、事件侦听器或代码中的其他地方完成的.
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();
问题出在 DoctrineORMInternalHydrationObjectHydrator 中,它具有属性initializedCollections",其中保存了已初始化的集合,并且集合由父实体类型和实体本身.不幸的是,在上述情况下,heydrator 不明白该集合在第一个查询中被过滤并在第二个查询中使用它以避免补液.(github链接)
The issue is in DoctrineORMInternalHydrationObjectHydrator, 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();
这篇关于Doctrine - OneToMany 关系,所有结果行不获取对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!