Pass a copy of the Carbon object to avoid unexpected side effects
This commit changes TimestampListener to pass a copy of the Carbon object representing the current time to the entity properties updatedAt and createdAt. Passing a copy instead of the object itself is needed to avoid unexpected side effects. Before this change, modifying the time of the createdAt or updatedAt field of a given entity could affect the same field of another entity as it could be the same object used inside TimestampListener for all entities. [MAILPOET-3870]
This commit is contained in:
@@ -27,11 +27,11 @@ class TimestampListener {
|
||||
&& method_exists($entity, 'getCreatedAt')
|
||||
&& !$entity->getCreatedAt()
|
||||
) {
|
||||
$entity->setCreatedAt($this->now);
|
||||
$entity->setCreatedAt($this->now->copy());
|
||||
}
|
||||
|
||||
if (in_array(UpdatedAtTrait::class, $entityTraits, true) && method_exists($entity, 'setUpdatedAt')) {
|
||||
$entity->setUpdatedAt($this->now);
|
||||
$entity->setUpdatedAt($this->now->copy());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ class TimestampListener {
|
||||
$entityTraits = $this->getEntityTraits($entity);
|
||||
|
||||
if (in_array(UpdatedAtTrait::class, $entityTraits, true) && method_exists($entity, 'setUpdatedAt')) {
|
||||
$entity->setUpdatedAt($this->now);
|
||||
$entity->setUpdatedAt($this->now->copy());
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user