Symfony Event Listener - populate entity

Asked
Active3 hr before
Viewed126 times

7 Answers

symfonylistenerevent
90%

I would suggest for the time being to implement your logic in the doctrine entity listener, and don't go around the Symfony event listener. (Actually, you are already using an event system, if you are not willing to do any other logic, I think it is sufficient to stay in that event system). – Andras Mar 22 '16 at 11:12 ,1, You will need to create a symfony2 event and fire that from your doctrine events. If you want to fire symfony2 events complicates the thing a bit, as you will need access to the framework event dispatcher, you will need to inject it to your LifeCycleEventListener. So:,2, I would consider checking if the status field changed at all, otherwise you will create a lot of CodeEvent() instances when any other property changes of the Project entity and the status is already 'closed_by_client' See here: http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/events.html#lifecycle-callbacks-event-argument,create your own Symfony event for this status change http://symfony.com/doc/current/components/event_dispatcher/introduction.html#creating-and-dispatching-an-event

Take into account that the prePersist and preFlush methods are within the Project class itself. In order to add it to your event's object you only need to use $this:

public
function PrePersist() {
   if ($this - > getCurrentStatus() == self::STATE_TO_BE_INDEXED_IN_ELA_CLOSED_BY_CLIENT) {
      $event = new CodeEvents();
      $event - > setProject($this);
   }
}
88%

Lifecycle callbacks, they are defined as public methods on the entity classes and they are called when the events are triggered;, Doctrine Lifecycle Listeners ,Lifecycle listeners and subscribers, they are classes with callback methods for one or more events and they are called for all entities;,Entity listeners, they are similar to lifecycle listeners, but they are called only for the entities of a certain class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// src/Entity/Product.php
namespace App\ Entity;

use Doctrine\ ORM\ Mapping as ORM;

// When using annotations, don't forget to add @ORM\HasLifecycleCallbacks()
// to the class of the entity where you define the callback

/**
 * @ORM\Entity()
 * @ORM\HasLifecycleCallbacks()
 */
class Product {
   // ...

   /**
    * @ORM\PrePersist
    */
   public
   function setCreatedAtValue(): void {
      $this - > createdAt = new\ DateTimeImmutable();
   }
}
load more v
72%

Step 5 − Create an event dispatcher object.,Step 1 − Create a project, event-dispatcher-example.,Step 4 − Create a listener, UserListener.,Step 3 − Create an event, UserRegisteredEvent.

Step 1 − Create a project, event-dispatcher-example.

cd / path / to / dir
mkdir event - dispatcher - example
cd event - dispatcher - example
composer require symfony / event - dispatcher
load more v
65%

Let's start that journey next.,One of the other "laws" of Symfony's event system is that a listener will always be passed a single argument: an event object. What type of object is it? This is where the new "event class names as event names" comes in handy. We're listening to RequestEvent, which means - surprise! - Symfony will pass us a RequestEvent object! Let's just dd($event)., 04. RequestEvent & RouterListener 6:26 , 02. Hooking into Symfony with an Event Subscriber 5:54

0
75%

This event is called after an entity is constructed by the EntityManager.,The Event SystemNaming convention,Doctrine invokes the listener resolver to get the listener instance.,The event system is controlled by the EventManager. It is the central point of Doctrine's event listener system. Listeners are registered on the manager and events are dispatched through the manager.

The event system is controlled by the EventManager. It is the central point of Doctrine's event listener system. Listeners are registered on the manager and events are dispatched through the manager.

1 < ? php
$evm = new EventManager();
2

Now we can add some event listeners to the $evm. Let's create a TestEvent class to play around with.

1 < ? php
class TestEvent {
   const preFoo = 'preFoo';
   const postFoo = 'postFoo';

   private $_evm;

   public $preFooInvoked = false;
   public $postFooInvoked = false;

   public
   function __construct($evm) {
      $evm - > addEventListener(array(self::preFoo, self::postFoo), $this);
   }

   public
   function preFoo(EventArgs $e) {
      $this - > preFooInvoked = true;
   }

   public
   function postFoo(EventArgs $e) {
      $this - > postFooInvoked = true;
   }
}

// Create a new instance
$test = new TestEvent($evm);
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

Events can be dispatched by using the dispatchEvent() method.

1 < ? php
$evm - > dispatchEvent(TestEvent::preFoo);
$evm - > dispatchEvent(TestEvent::postFoo);
2
3

You can easily remove a listener with the removeEventListener() method.

1 < ? php
$evm - > removeEventListener(array(self::preFoo, self::postFoo), $this);
2

The Doctrine ORM event system also has a simple concept of event subscribers. We can define a simple TestEventSubscriber class which implements the \Doctrine\Common\EventSubscriber interface and implements a getSubscribedEvents() method which returns an array of events it should be subscribed to.

1 < ? php
class TestEventSubscriber implements\ Doctrine\ Common\ EventSubscriber {
   public $preFooInvoked = false;

   public
   function preFoo() {
      $this - > preFooInvoked = true;
   }

   public
   function getSubscribedEvents() {
      return array(TestEvent::preFoo);
   }
}

$eventSubscriber = new TestEventSubscriber();
$evm - > addEventSubscriber($eventSubscriber);
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

Now when you dispatch an event, any event subscribers will be notified for that event.

1 < ? php
$evm - > dispatchEvent(TestEvent::preFoo);
2

Now you can test the $eventSubscriber instance to see if the preFoo() method was invoked.

1 < ? php
if ($eventSubscriber - > preFooInvoked) {
   echo 'pre foo invoked!';
}
2
3
4
load more v
40%

Maker bundle (for easy create entities and controllers),First things first, let's create a simple user entity we can use later on. We will use the Symfony maker bundle for this.,Takes advantage of symphony's authowire feature for registering events.,Next, we will create an event that will trigger a delete call and a subscriber to do all those steps.

composer require symfony / event - dispatcher
load more v
22%

Single entities and single Doctrine event. , Single entity and single Doctrine event. , All entities and single Doctrine event. , Separate class and configured in config.service.yaml.

Are Doctrine Lifecycle and Entity Listeners really only for a single event as I interpret the documentation states, or is it one method per Doctrine event such as the following?

App\ EventListener\ SearchIndexer:
   tags:
   -
   name: 'doctrine.event_listener'
event: 'postPersist' -
   name: 'doctrine.event_listener'
event: 'postUpdate'
load more v

Other "symfony-listener" queries related to "Symfony Event Listener - populate entity"