From dbf770f784b5bde79d09ab7fc68193601f236ffa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Wed, 20 May 2020 23:36:44 +0200 Subject: [PATCH] Update timestamp of the associated element. For example the lastModified timestamp of a part is now updated, when the user changes a price information. This should represent more what a user expects under a last Modified field and should improve also sorting... --- src/Entity/Attachments/Attachment.php | 8 ++++++++ src/Entity/Base/TimestampTrait.php | 2 +- src/Entity/Parameters/AbstractParameter.php | 8 ++++++++ src/Entity/PriceInformations/Orderdetail.php | 19 +++++++++++++++++++ src/Entity/PriceInformations/Pricedetail.php | 19 +++++++++++++++++++ .../LogSystem/EventLoggerSubscriber.php | 12 +++++++++++- .../BarcodeExampleElementsGenerator.php | 4 ++-- 7 files changed, 68 insertions(+), 4 deletions(-) diff --git a/src/Entity/Attachments/Attachment.php b/src/Entity/Attachments/Attachment.php index 1094cda9..dcc177a5 100644 --- a/src/Entity/Attachments/Attachment.php +++ b/src/Entity/Attachments/Attachment.php @@ -116,6 +116,14 @@ abstract class Attachment extends AbstractNamedDBElement } } + public function updateTimestamps(): void + { + parent::updateTimestamps(); + if ($this->element instanceof AttachmentContainingDBElement) { + $this->element->updateTimestamps(); + } + } + /*********************************************************** * Various function ***********************************************************/ diff --git a/src/Entity/Base/TimestampTrait.php b/src/Entity/Base/TimestampTrait.php index ba92cbac..f7a7a88f 100644 --- a/src/Entity/Base/TimestampTrait.php +++ b/src/Entity/Base/TimestampTrait.php @@ -92,7 +92,7 @@ trait TimestampTrait * @ORM\PrePersist * @ORM\PreUpdate */ - public function updatedTimestamps(): void + public function updateTimestamps(): void { $this->lastModified = new DateTime('now'); if (null === $this->addedDate) { diff --git a/src/Entity/Parameters/AbstractParameter.php b/src/Entity/Parameters/AbstractParameter.php index 7ee73c92..9584ed70 100644 --- a/src/Entity/Parameters/AbstractParameter.php +++ b/src/Entity/Parameters/AbstractParameter.php @@ -120,6 +120,14 @@ abstract class AbstractParameter extends AbstractNamedDBElement } } + public function updateTimestamps(): void + { + parent::updateTimestamps(); + if ($this->element instanceof AbstractNamedDBElement) { + $this->element->updateTimestamps(); + } + } + /** * Returns the element this parameter belongs to. * diff --git a/src/Entity/PriceInformations/Orderdetail.php b/src/Entity/PriceInformations/Orderdetail.php index 47931275..ccf0fb1e 100644 --- a/src/Entity/PriceInformations/Orderdetail.php +++ b/src/Entity/PriceInformations/Orderdetail.php @@ -56,6 +56,7 @@ use App\Entity\Contracts\NamedElementInterface; use App\Entity\Contracts\TimeStampableInterface; use App\Entity\Parts\Part; use App\Entity\Parts\Supplier; +use DateTime; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; @@ -134,6 +135,24 @@ class Orderdetail extends AbstractDBElement implements TimeStampableInterface, N parent::__clone(); } + /** + * Helper for updating the timestamp. It is automatically called by doctrine before persisting. + * + * @ORM\PrePersist + * @ORM\PreUpdate + */ + public function updateTimestamps(): void + { + $this->lastModified = new DateTime('now'); + if (null === $this->addedDate) { + $this->addedDate = new DateTime('now'); + } + + if ($this->part instanceof Part) { + $this->part->updateTimestamps(); + } + } + /******************************************************************************** * * Getters diff --git a/src/Entity/PriceInformations/Pricedetail.php b/src/Entity/PriceInformations/Pricedetail.php index 9e391063..e19e78b2 100644 --- a/src/Entity/PriceInformations/Pricedetail.php +++ b/src/Entity/PriceInformations/Pricedetail.php @@ -57,6 +57,7 @@ use App\Validator\Constraints\BigDecimal\BigDecimalPositive; use App\Validator\Constraints\Selectable; use Brick\Math\BigDecimal; use Brick\Math\RoundingMode; +use DateTime; use Doctrine\ORM\Mapping as ORM; use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; use Symfony\Component\Validator\Constraints as Assert; @@ -132,6 +133,24 @@ class Pricedetail extends AbstractDBElement implements TimeStampableInterface parent::__clone(); } + /** + * Helper for updating the timestamp. It is automatically called by doctrine before persisting. + * + * @ORM\PrePersist + * @ORM\PreUpdate + */ + public function updateTimestamps(): void + { + $this->lastModified = new DateTime('now'); + if (null === $this->addedDate) { + $this->addedDate = new DateTime('now'); + } + + if ($this->orderdetail instanceof Orderdetail) { + $this->orderdetail->updateTimestamps(); + } + } + /******************************************************************************** * * Getters diff --git a/src/EventSubscriber/LogSystem/EventLoggerSubscriber.php b/src/EventSubscriber/LogSystem/EventLoggerSubscriber.php index 281d0a12..484ef53c 100644 --- a/src/EventSubscriber/LogSystem/EventLoggerSubscriber.php +++ b/src/EventSubscriber/LogSystem/EventLoggerSubscriber.php @@ -172,7 +172,8 @@ class EventLoggerSubscriber implements EventSubscriber $em = $args->getEntityManager(); $uow = $em->getUnitOfWork(); // If the we have added any ElementCreatedLogEntries added in postPersist, we flush them here. - if ($uow->hasPendingInsertions()) { + $uow->computeChangeSets(); + if ($uow->hasPendingInsertions() || !empty($uow->getScheduledEntityUpdates())) { $em->flush(); } @@ -266,6 +267,15 @@ class EventLoggerSubscriber implements EventSubscriber { $uow = $em->getUnitOfWork(); + /* We have to call that here again, so the foreign entity timestamps, that were changed in updateTimestamp + get persisted */ + $changeSet = $uow->getEntityChangeSet($entity); + + //Skip log entry, if only the lastModified field has changed... + if (isset($changeSet['lastModified']) && count($changeSet)) { + return; + } + $log = new ElementEditedLogEntry($entity); if ($this->save_changed_data) { $this->saveChangeSet($entity, $log, $em); diff --git a/src/Services/LabelSystem/Barcodes/BarcodeExampleElementsGenerator.php b/src/Services/LabelSystem/Barcodes/BarcodeExampleElementsGenerator.php index 118b1a24..29150c17 100644 --- a/src/Services/LabelSystem/Barcodes/BarcodeExampleElementsGenerator.php +++ b/src/Services/LabelSystem/Barcodes/BarcodeExampleElementsGenerator.php @@ -62,7 +62,7 @@ final class BarcodeExampleElementsGenerator $part->setManufacturerProductNumber('CUSTOM MPN'); $part->setTags('Tag1, Tag2, Tag3'); $part->setManufacturingStatus('active'); - $part->updatedTimestamps(); + $part->updateTimestamps(); $part->setFavorite(true); $part->setMinAmount(100); @@ -90,7 +90,7 @@ final class BarcodeExampleElementsGenerator $storelocation = new Storelocation(); $storelocation->setName('Location 1'); $storelocation->setComment('Example comment'); - $storelocation->updatedTimestamps(); + $storelocation->updateTimestamps(); $parent = new Storelocation(); $parent->setName('Parent');