Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php ORM原则-计算“ORM”值的事件订阅者;“子实体”;并在另一个实体上保留该值_Php_Symfony_Doctrine Orm_Doctrine - Fatal编程技术网

Php ORM原则-计算“ORM”值的事件订阅者;“子实体”;并在另一个实体上保留该值

Php ORM原则-计算“ORM”值的事件订阅者;“子实体”;并在另一个实体上保留该值,php,symfony,doctrine-orm,doctrine,Php,Symfony,Doctrine Orm,Doctrine,我有点困惑应该在哪里实现事件订阅服务器(实际上,我甚至不确定是否应该使用实体侦听器) 因此,我有一个名为servicedoctrinetity的实体,这个实体有许多存储设备: class ServiceDoctrineEntity { /** * One Service has many storage devices. * * @OneToMany(targetEntity="ServiceStorageDevicesDoctrineEntity", m

我有点困惑应该在哪里实现事件订阅服务器(实际上,我甚至不确定是否应该使用实体侦听器)

因此,我有一个名为
servicedoctrinetity
的实体,这个实体有许多存储设备:

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;
}
每个存储设备都有自己的“存储空间/价值”

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

  • 我需要计算此服务拥有的“存储设备”总数
  • 聚合或求和每个存储设备的总“值”
  • 这些“摘要”将保存在名为:
    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;
    
    }
    
    我试图为此编写一个类,用于侦听
    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`?
            }
        }
    }
    
    但我不知道如何继续:

  • 我使用事件订阅服务器对吗
  • 我在
    onFlush
    事件中的收听正确吗
  • 我应该向哪个实体列出:

    (a)
    ServiceDoctrine
    实体和循环通过每个存储设备

    (b) 或者转到
    ServiceStorageDevicesDoctrineEntity
    ,但这不意味着计算逻辑将针对每个存储设备更改运行吗

  • 我使用事件订阅服务器对吗

    对。如果您想在某个事件发生后执行某些逻辑,那么您正处于正确的位置

    EventSubscriber
    为您提供了更大的灵活性,因为您可以侦听多个条令事件(其中
    EventListener
    通常与特定事件关联)

    我听onFlush事件对吗

    onFlush
    事件发生在计算所有实体更改集之后

    由于要更新相关实体的数据,我建议您收听以下事件:

    • prePersist
    • 预更新
    • preRemove
    我应该向哪个实体上市

    收听
    serviceStorageSummaryDoctrinentity
    意味着您可以在更改此实体时执行某些操作。但由于您从未手动更新此实体,因此将永远不会触发事件

    相反,您要做的是在
    ServiceStorageDevicesDoctrineEntity
    发生更改时监视更改

    您的EventListener可能如下所示:

    class ServiceStorageDevicesEventListener implements EventSubscriber {
        public function getSubscribedEvents() {
            return array(
                Events::prePersist,
                Events::preUpdate,
                Events::preRemove,
            );
        }
    
        public function prePersist(LifecycleEventArgs $args)
        {
            $entity = $args->getObject();
    
            // don't perform operations for other entities
            if (!$entity instanceof ServiceStorageDevicesDoctrineEntity) {
               return;
            }
    
            // update your related entity here, ex:
            $entity->getServiceStorageDevices()->getServiceStorageSummary()->setTotalValue(10);
        }
    
        public function preUpdate(PreUpdateEventArgs $args)
        {
            // ...
        }
    
        public function preRemove(LifecycleEventArgs $args)
        {
            // ...
        }
    }
    

    希望能有所帮助。

    我通过保持
    刷新
    事件解决了我的问题,到目前为止,我没有收到任何问题,但如果我有问题,我会尝试使用您的解决方案。
    class ServiceStorageDevicesEventListener implements EventSubscriber {
        public function getSubscribedEvents() {
            return array(
                Events::prePersist,
                Events::preUpdate,
                Events::preRemove,
            );
        }
    
        public function prePersist(LifecycleEventArgs $args)
        {
            $entity = $args->getObject();
    
            // don't perform operations for other entities
            if (!$entity instanceof ServiceStorageDevicesDoctrineEntity) {
               return;
            }
    
            // update your related entity here, ex:
            $entity->getServiceStorageDevices()->getServiceStorageSummary()->setTotalValue(10);
        }
    
        public function preUpdate(PreUpdateEventArgs $args)
        {
            // ...
        }
    
        public function preRemove(LifecycleEventArgs $args)
        {
            // ...
        }
    }