diff --git a/lib/Doctrine/EventListeners/TimestampListener.php b/lib/Doctrine/EventListeners/TimestampListener.php index 25735010a3..f2cd22686a 100644 --- a/lib/Doctrine/EventListeners/TimestampListener.php +++ b/lib/Doctrine/EventListeners/TimestampListener.php @@ -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()); } } diff --git a/tests/integration/Doctrine/EventListeners/TimestampListenerTest.php b/tests/integration/Doctrine/EventListeners/TimestampListenerTest.php index 0408847bb3..675ff720f4 100644 --- a/tests/integration/Doctrine/EventListeners/TimestampListenerTest.php +++ b/tests/integration/Doctrine/EventListeners/TimestampListenerTest.php @@ -72,6 +72,26 @@ class TimestampListenerTest extends \MailPoetTest { expect($entity->getUpdatedAt())->equals($this->now); } + public function testItUsesDifferentTimesWhenCreatingDifferentEntities() { + $entity1 = new TimestampEntity(); + $entity1->setName('Entity 1'); + + $this->entityManager->persist($entity1); + $this->entityManager->flush(); + + $createdAt1 = $entity1->getCreatedAt(); + $this->assertInstanceOf(Carbon::class, $createdAt1); + $createdAt1->subMonth(); + + $entity2 = new TimestampEntity(); + $entity2->setName('Entity 2'); + + $this->entityManager->persist($entity2); + $this->entityManager->flush(); + + $this->assertEquals($this->now, $entity2->getCreatedAt()); + } + public function _after() { parent::_after(); $this->connection->executeStatement("DROP TABLE IF EXISTS $this->tableName");