Doctrine ORM-事件订阅服务器,它计算“子实体”的值并将其保留在另一个实体上

问题描述 投票:0回答:1

我对在哪里实现事件订阅服务器感到困惑(实际上,我什至不确定是否应该使用实体监听器)。

因此,我有一个称为ServiceDoctrineEntity的实体,并且该实体有很多存储设备:

class ServiceDoctrineEntity {

    /**
     * One Service has many storage devices.
     *
     * @OneToMany(targetEntity="ServiceStorageDevicesDoctrineEntity", mappedBy="service", cascade={"all"})
     * @OrderBy({"order" = "ASC"})
     * @var ServiceStorageDevicesDoctrineEntity $storage_devices Description.
     */
    private $storage_devices;
}

并且每个存储设备都有其自己的“存储空间/值”

class ServiceStorageDevicesDoctrineEntity {
    /**
     * @Column(type="bigint", nullable=true, options={"comment":"The normalised value, calculated from $amount * $unit."})
     * @var int $value The normalised value, calculated from $amount * $unit.
     */
    private $value;
}

我的问题是,我应该运行一个计算逻辑,该逻辑将在每次存储设备发生更改时触发(添加/删除新的存储设备或更改存储空间等)。

  1. 我需要计算此服务具有的“存储设备”总数。
  2. 汇总或汇总每个存储设备的总“值”

这些“摘要”将保存在名为:ServiceStorageSummaryDoctrineEntity]的实体上

class ServiceStorageSummaryDoctrineEntity {

    /**
     * One Service Support type has One Service.
     *
     * @OneToOne(targetEntity="ServiceDoctrineEntity", inversedBy="storage_summary")
     * @JoinColumn(name="service_id", referencedColumnName="id")
     */
    private $service;

    /**
     * @Column(type="bigint", nullable=true, options={"default":0, "comment":"The total value (normalised) of storage space."})
     * @var integer $total_value The total value (normalised) of storage space.
     */
    private $total_value;

    /**
     * @Column(type="integer", nullable=true, options={"default":0, "comment":"The total amount of storage devices."})
     * @var integer $device_count The total amount of storage devices.
     */
    private $device_count;

}

我试图为此编写一个EventSubscriber类,以监听EventSubscriber事件:

onFlush

但是我不确定如何进行此操作:

  1. 我可以使用事件订阅服务器吗?
  2. 我是否有权听class ServiceStorageDevicesEventListener implements EventSubscriber { /** * Returns an array of events this subscriber wants to listen to. * * @return string[] */ public function getSubscribedEvents() { return array( Events::onFlush, ); } public function onFlush( OnFlushEventArgs $eventArgs ) { $em = $eventArgs->getEntityManager(); $uow = $em->getUnitOfWork(); foreach ( $uow->getScheduledEntityInsertions() as $entity ) { // Should I run the computation here? // Which entity should I be listening to? `ServiceDoctrineEntity` or `ServiceStorageSummaryDoctrineEntity`? } foreach ( $uow->getScheduledEntityUpdates() as $entity ) { // Should I run the computation here? // Which entity should I be listening to? `ServiceDoctrineEntity` or `ServiceStorageSummaryDoctrineEntity`? } foreach ( $uow->getScheduledEntityDeletions() as $entity ) { // Should I run the computation here? // Which entity should I be listening to? `ServiceDoctrineEntity` or `ServiceStorageSummaryDoctrineEntity`? } } } 事件
  3. 我应该列出哪个实体:

  4. ((a)onFlush实体并遍历每个存储设备?

    ((b)或ServiceDoctrine,但这是否意味着计算逻辑将针对每个存储设备更改而运行?

我对应该在哪里实现事件订阅器感到困惑(实际上,我什至不确定我是否应该使用实体监听器)。因此,我有一个名为ServiceDoctrineEntity的实体,并且...

php symfony doctrine-orm doctrine
1个回答
1
投票

我可以使用事件订阅服务器吗?

© www.soinside.com 2019 - 2024. All rights reserved.