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...
This commit is contained in:
Jan Böhmer 2020-05-20 23:36:44 +02:00
parent 2b21d4039a
commit dbf770f784
7 changed files with 68 additions and 4 deletions

View file

@ -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 * Various function
***********************************************************/ ***********************************************************/

View file

@ -92,7 +92,7 @@ trait TimestampTrait
* @ORM\PrePersist * @ORM\PrePersist
* @ORM\PreUpdate * @ORM\PreUpdate
*/ */
public function updatedTimestamps(): void public function updateTimestamps(): void
{ {
$this->lastModified = new DateTime('now'); $this->lastModified = new DateTime('now');
if (null === $this->addedDate) { if (null === $this->addedDate) {

View file

@ -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. * Returns the element this parameter belongs to.
* *

View file

@ -56,6 +56,7 @@ use App\Entity\Contracts\NamedElementInterface;
use App\Entity\Contracts\TimeStampableInterface; use App\Entity\Contracts\TimeStampableInterface;
use App\Entity\Parts\Part; use App\Entity\Parts\Part;
use App\Entity\Parts\Supplier; use App\Entity\Parts\Supplier;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
@ -134,6 +135,24 @@ class Orderdetail extends AbstractDBElement implements TimeStampableInterface, N
parent::__clone(); 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 * Getters

View file

@ -57,6 +57,7 @@ use App\Validator\Constraints\BigDecimal\BigDecimalPositive;
use App\Validator\Constraints\Selectable; use App\Validator\Constraints\Selectable;
use Brick\Math\BigDecimal; use Brick\Math\BigDecimal;
use Brick\Math\RoundingMode; use Brick\Math\RoundingMode;
use DateTime;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert; use Symfony\Component\Validator\Constraints as Assert;
@ -132,6 +133,24 @@ class Pricedetail extends AbstractDBElement implements TimeStampableInterface
parent::__clone(); 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 * Getters

View file

@ -172,7 +172,8 @@ class EventLoggerSubscriber implements EventSubscriber
$em = $args->getEntityManager(); $em = $args->getEntityManager();
$uow = $em->getUnitOfWork(); $uow = $em->getUnitOfWork();
// If the we have added any ElementCreatedLogEntries added in postPersist, we flush them here. // 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(); $em->flush();
} }
@ -266,6 +267,15 @@ class EventLoggerSubscriber implements EventSubscriber
{ {
$uow = $em->getUnitOfWork(); $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); $log = new ElementEditedLogEntry($entity);
if ($this->save_changed_data) { if ($this->save_changed_data) {
$this->saveChangeSet($entity, $log, $em); $this->saveChangeSet($entity, $log, $em);

View file

@ -62,7 +62,7 @@ final class BarcodeExampleElementsGenerator
$part->setManufacturerProductNumber('CUSTOM MPN'); $part->setManufacturerProductNumber('CUSTOM MPN');
$part->setTags('Tag1, Tag2, Tag3'); $part->setTags('Tag1, Tag2, Tag3');
$part->setManufacturingStatus('active'); $part->setManufacturingStatus('active');
$part->updatedTimestamps(); $part->updateTimestamps();
$part->setFavorite(true); $part->setFavorite(true);
$part->setMinAmount(100); $part->setMinAmount(100);
@ -90,7 +90,7 @@ final class BarcodeExampleElementsGenerator
$storelocation = new Storelocation(); $storelocation = new Storelocation();
$storelocation->setName('Location 1'); $storelocation->setName('Location 1');
$storelocation->setComment('Example comment'); $storelocation->setComment('Example comment');
$storelocation->updatedTimestamps(); $storelocation->updateTimestamps();
$parent = new Storelocation(); $parent = new Storelocation();
$parent->setName('Parent'); $parent->setName('Parent');