2017-05-29 8 views
0

サードパーティのバンドルエンティティに追加のロジックを追加します。このエンティティには、追加機能と削除機能があります。既存のエンティティを拡張して追加機能と削除機能をオーバーライドします

このエンティティを拡張し、追加と削除の機能を無効にする方法はありますか?

これは私の拡張エンティティです。私は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; 
 
    } 
 
}

+0

まだ問題がある場合は、http://symfony.com/doc/current/bundles/inheritance.htmlを読んで質問を更新してください。 – lordrhodos

+0

私はそれを読んだ。エンティティファイルを除いて、バンドルの他のすべての部分をオーバーライドすることができます。 –

+0

ああ、私はあなたが教義のエンティティのマッピングを上書きすることはできないと思います。 [クラスはパラメータとして定義されています](https://github.com/orocrm/OroCalendarBundle/blob/master/Resources/config/services.yml#L3)、一部の部分でのみ使用されています。他の部分は直接インスタンス化するので、クラスを "交換"したい場合は、多くの部分をオーバーライドする必要があります。 イベントに出席者が追加された後にログエントリを追加するだけですか? – lordrhodos

答えて

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を作成して、プロセス内の関数を起動します。

+0

ありがとうございました!これは私が探しているものです。 –

関連する問題