我对在哪里实现事件订阅服务器感到困惑(实际上,我什至不确定是否应该使用实体监听器)。
因此,我有一个称为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;
}
我的问题是,我应该运行一个计算逻辑,该逻辑将在每次存储设备发生更改时触发(添加/删除新的存储设备或更改存储空间等)。
这些“摘要”将保存在名为: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
但是我不确定如何进行此操作:
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`?
}
}
}
事件我应该列出哪个实体:
((a)onFlush
实体并遍历每个存储设备?
((b)或ServiceDoctrine
,但这是否意味着计算逻辑将针对每个存储设备更改而运行?
我对应该在哪里实现事件订阅器感到困惑(实际上,我什至不确定我是否应该使用实体监听器)。因此,我有一个名为ServiceDoctrineEntity的实体,并且...
我可以使用事件订阅服务器吗?