Doctrine - OneToMany 关系,所有结果行都不会在对象中获取 [英] Doctrine - OneToMany relation, all result row doesn't fetch in object

查看:23
本文介绍了Doctrine - OneToMany 关系,所有结果行都不会在对象中获取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将我的所有对象 DemandCab 与他们的子对象 (DecisionCab) 一起获取.

I try to get all my objects DemandCab with their children object (DecisionCab).

我的两个实体

/**
 * 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 ...

查询很好,但我不明白为什么 hydration 删除 DecisionCab 对象,其属性 decision 为 0 或 1(仅显示空值).

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.

无论如何,我的假设是您在查询构建器的帮助下查询与决定关系连接的需求实体至少一次.也许这是在您的操作、事件侦听器或代码中的其他地方完成的.

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 关系,所有结果行都不会在对象中获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆