From ea9b00a8025f8bbe7a1c28399f7538bea6d60a2c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Gertz?= Date: Mon, 23 Aug 2021 09:07:47 +0200 Subject: [PATCH] Add doctrine preUpdate lifecycleCallbacks on civility and group --- Entity/Civility.php | 12 ++++++++++++ Entity/Group.php | 12 ++++++++++++ Resources/config/doctrine/Civility.orm.yml | 2 ++ Resources/config/doctrine/Group.orm.yml | 2 ++ 4 files changed, 28 insertions(+) diff --git a/Entity/Civility.php b/Entity/Civility.php index f350919..4d2671a 100644 --- a/Entity/Civility.php +++ b/Entity/Civility.php @@ -12,6 +12,7 @@ namespace Rapsys\UserBundle\Entity; use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\ORM\Event\PreUpdateEventArgs; use Rapsys\UserBundle\Entity\User; @@ -160,6 +161,17 @@ class Civility { return $this->users; } + /** + * {@inheritdoc} + */ + public function preUpdate(PreUpdateEventArgs $eventArgs) { + //Check that we have a civility instance + if (($user = $eventArgs->getEntity()) instanceof Civility) { + //Set updated value + $user->setUpdated(new \DateTime('now')); + } + } + /** * Returns a string representation of the title * diff --git a/Entity/Group.php b/Entity/Group.php index d7ca3c7..188e1bc 100644 --- a/Entity/Group.php +++ b/Entity/Group.php @@ -12,6 +12,7 @@ namespace Rapsys\UserBundle\Entity; use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\ORM\Event\PreUpdateEventArgs; use Rapsys\UserBundle\Entity\User; @@ -160,6 +161,17 @@ class Group { return $this->users; } + /** + * {@inheritdoc} + */ + public function preUpdate(PreUpdateEventArgs $eventArgs) { + //Check that we have a group instance + if (($user = $eventArgs->getEntity()) instanceof Group) { + //Set updated value + $user->setUpdated(new \DateTime('now')); + } + } + /** * Returns a string representation of the group * diff --git a/Resources/config/doctrine/Civility.orm.yml b/Resources/config/doctrine/Civility.orm.yml index a175fa8..6ed7944 100644 --- a/Resources/config/doctrine/Civility.orm.yml +++ b/Resources/config/doctrine/Civility.orm.yml @@ -19,3 +19,5 @@ Rapsys\UserBundle\Entity\Civility: # users: # targetEntity: User # mappedBy: title + lifecycleCallbacks: + preUpdate: ['preUpdate'] diff --git a/Resources/config/doctrine/Group.orm.yml b/Resources/config/doctrine/Group.orm.yml index af56dbe..ee28e6c 100644 --- a/Resources/config/doctrine/Group.orm.yml +++ b/Resources/config/doctrine/Group.orm.yml @@ -19,3 +19,5 @@ Rapsys\UserBundle\Entity\Group: # users: # targetEntity: User # mappedBy: groups + lifecycleCallbacks: + preUpdate: ['preUpdate'] -- 2.41.1