Doctrine - OneToMany 关系,所有结果行不获取对象

2024-08-10php开发问题
3

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

问题描述

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

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17