扩展现有实体以覆盖添加和删除功能

时间:2017-05-29 05:32:23

标签: symfony doctrine-orm

我想向第三方捆绑实体添加其他逻辑。在这个实体中有添加和删除功能。

有没有办法扩展此实体并覆盖添加和删除功能?

这是我的扩展实体。我试着扩展Oro\Bundle\CalendarBundle\Entity\CalendarEvent。当我运行它时,我的扩展实体不会被调用。



<?php

namespace CampusCRM\CampusCalendarBundle\Entity;

use Oro\Bundle\CalendarBundle\Entity\CalendarEvent as BaseEntity;
use Oro\Bundle\CalendarBundle\Entity\Attendee;

class CalendarEvent extends BaseEntity
{
    /**
     * CalendarEvent constructor.
     */
    public function __construct()
    {
        parent::__construct();
    }

        /**
     * Add attendee of Calendar Event. This method should not be called using child event.
     *
     * @param Attendee $attendee
     * @return CalendarEvent
     * @throws \LogicException If method is called with child event.
     */
    public function addAttendee(Attendee $attendee)
    {
        parent::addAttendee($attendee);

        file_put_contents('/tmp/freq.log', 'add attendee '  . PHP_EOL, FILE_APPEND);
        
        return $this;
    }
}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

如果您想在添加或删除与会者时挂钩,我认为您需要查看oro_calendar.listener.calendar_event_attendees服务:

# OroCalendarBundle/Resources/config/services.yml
services:
    oro_calendar.listener.calendar_event_attendees:
        class: Oro\Bundle\CalendarBundle\EventListener\CalendarEventAttendeesListener
        tags:
            - { name: doctrine.event_listener, event: onFlush }

以及相关的CalendarEventAttendeesListener类:

<?php

namespace Oro\Bundle\CalendarBundle\EventListener;

use Doctrine\ORM\Event\OnFlushEventArgs;
use Doctrine\ORM\UnitOfWork;

use Oro\Bundle\CalendarBundle\Entity\Attendee;
use Oro\Bundle\CalendarBundle\Entity\CalendarEvent;
use Oro\Bundle\PlatformBundle\EventListener\OptionalListenerInterface;

class CalendarEventAttendeesListener implements OptionalListenerInterface
{
    /** @var bool */
    protected $enabled = true;
/**
 * @param OnFlushEventArgs $args
 */
public function onFlush(OnFlushEventArgs $args)
{
    if (!$this->enabled) {
        return;
    }

    $entityManager = $args->getEntityManager();
    $unitOfWork = $entityManager->getUnitOfWork();

    $newEntities = $unitOfWork->getScheduledEntityInsertions();
    $updateEntities = $unitOfWork->getScheduledEntityUpdates();
    $deletedEntities = $unitOfWork->getScheduledEntityDeletions();

    foreach ($newEntities as $entity) {
        if ($this->isAttendeeApplicable($entity, $unitOfWork)) {
            $this->updateCalendarEventUpdatedAt($entity->getCalendarEvent(), $unitOfWork);
        }
    }
    foreach ($updateEntities as $entity) {
        if ($this->isAttendeeApplicable($entity, $unitOfWork)) {
            $this->updateCalendarEventUpdatedAt($entity->getCalendarEvent(), $unitOfWork);
        }
    }
    foreach ($deletedEntities as $entity) {
        if ($this->isAttendeeApplicable($entity, $unitOfWork)
            && !$unitOfWork->isScheduledForDelete($entity->getCalendarEvent())
        ) {
            $this->updateCalendarEventUpdatedAt($entity->getCalendarEvent(), $unitOfWork);
        }
    }
}

/**
 * @param object $entity
 *
 * @return bool
 */
protected function isAttendeeApplicable($entity, UnitOfWork $unitOfWork)
{
    return $entity instanceof Attendee
        && $entity->getCalendarEvent()
        && !$entity->getCalendarEvent()->isUpdatedAtSet()
        && count($unitOfWork->getEntityChangeSet($entity->getCalendarEvent())) == 0;
}

/**
 * @param CalendarEvent $calendarEvent
 * @param UnitOfWork    $unitOfWork
 */
protected function updateCalendarEventUpdatedAt(CalendarEvent $calendarEvent, UnitOfWork $unitOfWork)
{
    $oldUpdatedAt = $calendarEvent->getUpdatedAt();
    $newUpdatedAt = new \DateTime('now', new \DateTimeZone('UTC'));

    $calendarEvent->setUpdatedAt($newUpdatedAt);
    $unitOfWork->propertyChanged($calendarEvent, 'updatedAt', $oldUpdatedAt, $newUpdatedAt);
}

/**
 * {@inheritdoc}
 */
public function setEnabled($enabled = true)
{
    $this->enabled = $enabled;
}

}

覆盖onFlush方法或使用类似方法创建自己的EventListener以在此过程中触发您的函数。