关联的学说 postLoad 事件

2023-07-14php开发问题
3

本文介绍了关联的学说 postLoad 事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我目前有一个实体,我想在加载时稍微修改它.此修改将是一次性更改,然后将与实体一起保存在新字段中.

I currently have an entity which I would like to modify slightly upon load. This modification will be a one time change which will then be persisted in a new field along with the entity.

澄清我当前的目标:实体是一个位置"并且构成嵌套集的一部分.它有一个名称、lft/rgt 值和一个 Id.我使用这个实体执行的一项计算成本很高的任务是获取完整的位置路径并将其显示为文本.例如,对于位置实体滑铁卢",我想显示为滑铁卢|伦敦|英国".这涉及遍历整个集合(到根节点).

To clarify my current objective: The entity is a "Location" and forms part of a nested set. It has a name, lft/rgt values and an Id. One computationally expensive task I was performing with this entity was to fetch a full location path and display it as text. For example, with the location entity "Waterloo" I want to display as "Waterloo|London|United Kingdom". This involves traversing through the entire set (to the root node).

为了降低成本,我在 Location 实体上创建了一个新字段,该字段可以使用此值进行标记(并在修改位置(或树中的任何位置)名称时进行更新).考虑到我的应用程序处于活动状态,我需要避免将其作为一次性进程运行,因为它会在数据库上产生相当密集的一次性命中,相反,我想在每个位置(没有该值)已加载.我认为 Doctrine 的 postLoad 事件机制非常适合实现这一目标,但是..

To reduce the cost of this I've created a new field on the Location entity that can be stamped with this value (and updated as/when the location (or any location within the tree) name is modified). Considering my application is in a live state I need to avoid running this as a one off process as it would incur quite an intensive one-time hit on the DB, instead I'd like to apply this update as and when each location (without that value) is loaded. I assumed Doctrine's postLoad event mechanism would be perfect for achieving this, however..

位置实体不是由我的应用程序直接加载的,它们将始终是关系的反面.有了这个想法,以及学说的 postLoad 事件的事实:

The Location entities are not loaded directly by my application, they will always be the inverse side of a relation. With this is mind, and the fact that doctrine's postLoad event:

  • 不加载(允许访问)任何相关数据
  • 仅因拥有实体而被解雇

我无法温和地进行这些修改.

I have no way of gently making these modifications.

有人对此有任何建议或经验吗?

Anyone have any advice, or experience on this?

推荐答案

通过使用实体管理器上的 initializeObject() 方法,我能够在 postLoad 事件中加载关联的 Location 对象.

I was able to load the associated Location objects within the postLoad event by using the initializeObject() method on the Entity Manager.

/**
 * Upon loading the object, if the location text isn't set, set it
 * @param DoctrineORMEventLifecycleEventArgs $args
 */
public function postLoad(DoctrineORMEventLifecycleEventArgs $args)
{
    $this->em = $args->getEntityManager();
    $entity = $args->getEntity();

    if ($entity instanceof EntitiesLocation)
    {
        if (is_null($entity->getPathText()))
        {
            $entity->setPathText("new value");
            $this->em->flush($entity);
        }
    } elseif ($entity instanceof {parent Entity})
    {
        $location = $entity->getLocation();
        // This triggers the postLoad event again, but this time with Location as the parent Entity
        $this->em->initializeObject($location);
    }
}

这篇关于关联的学说 postLoad 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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