沃梦达 / 编程问答 / php问题 / 正文

关联的学说 postLoad 事件

Doctrine postLoad event for associations(关联的学说 postLoad 事件)

本文介绍了关联的学说 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 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:关联的学说 postLoad 事件

基础教程推荐