Skip to content

[Doctrine] Replace deprecated Doctrine\ORM\Event\LifecycleEventArgs #18145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions doctrine/events.rst
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,13 @@ do so, define a listener for the ``postPersist`` Doctrine event::
namespace App\EventListener;

use App\Entity\Product;
use Doctrine\Persistence\Event\LifecycleEventArgs;
use Doctrine\ORM\Event\PostPersistEventArgs;

class SearchIndexer
{
// the listener methods receive an argument which gives you access to
// both the entity object of the event and the entity manager itself
public function postPersist(LifecycleEventArgs $args): void
public function postPersist(PostPersistEventArgs $args): void
{
$entity = $args->getObject();

Expand Down Expand Up @@ -167,12 +167,12 @@ listener in the Symfony application by creating a new service for it and
namespace App\EventListener;

use Doctrine\Bundle\DoctrineBundle\Attribute\AsDoctrineListener;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Event\PostPersistEventArgs;

#[AsDoctrineListener('postPersist'/*, 500, 'default'*/)]
class SearchIndexer
{
public function postPersist(LifecycleEventArgs $event): void
public function postPersist(PostPersistEventArgs $event): void
{
// ...
}
Expand Down Expand Up @@ -277,13 +277,13 @@ First, define a PHP class that handles the ``postUpdate`` Doctrine event::
namespace App\EventListener;

use App\Entity\User;
use Doctrine\Persistence\Event\LifecycleEventArgs;
use Doctrine\ORM\Event\PostUpdateEventArgs;

class UserChangedNotifier
{
// the entity listener methods receive two arguments:
// the entity instance and the lifecycle event
public function postUpdate(User $user, LifecycleEventArgs $event): void
public function postUpdate(User $user, PostUpdateEventArgs $event): void
{
// ... do something to notify the changes
}
Expand Down Expand Up @@ -420,7 +420,9 @@ want to log all the database activity. To do so, define a subscriber for the
use App\Entity\Product;
use Doctrine\Bundle\DoctrineBundle\EventSubscriber\EventSubscriberInterface;
use Doctrine\ORM\Events;
use Doctrine\Persistence\Event\LifecycleEventArgs;
use Doctrine\ORM\Event\PostPersistEventArgs;
use Doctrine\ORM\Event\PostRemoveEventArgs;
use Doctrine\ORM\Event\PostUpdateEventArgs;

class DatabaseActivitySubscriber implements EventSubscriberInterface
{
Expand All @@ -436,24 +438,24 @@ want to log all the database activity. To do so, define a subscriber for the
}

// callback methods must be called exactly like the events they listen to;
// they receive an argument of type LifecycleEventArgs, which gives you access
// they receive an argument of type PostPersistEventArgs, which gives you access
// to both the entity object of the event and the entity manager itself
public function postPersist(LifecycleEventArgs $args): void
public function postPersist(PostPersistEventArgs $args): void
{
$this->logActivity('persist', $args);
}

public function postRemove(LifecycleEventArgs $args): void
public function postRemove(PostRemoveEventArgs $args): void
{
$this->logActivity('remove', $args);
}

public function postUpdate(LifecycleEventArgs $args): void
public function postUpdate(PostUpdateEventArgs $args): void
{
$this->logActivity('update', $args);
}

private function logActivity(string $action, LifecycleEventArgs $args): void
private function logActivity(string $action, PostUpdateEventArgs|PostRemoveEventArgs|PostPersistEventArgs $args): void
{
$entity = $args->getObject();

Expand Down