diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadata.php b/lib/Doctrine/ORM/Mapping/ClassMetadata.php index 027f7a37df..b821f88fdf 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadata.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadata.php @@ -526,7 +526,7 @@ protected function validateAndCompleteVersionFieldMapping(VersionFieldMetadata $ return; } - if ($property->getTypeName() === 'datetime') { + if (in_array($property->getTypeName(), ['datetime', 'datetime_immutable', 'datetimetz', 'datetimetz_immutable'], true)) { $property->setOptions(array_merge($options, ['default' => 'CURRENT_TIMESTAMP'])); return; diff --git a/tests/Doctrine/Tests/ORM/Functional/Locking/OptimisticTest.php b/tests/Doctrine/Tests/ORM/Functional/Locking/OptimisticTest.php index 860ccc165e..9b12c3a344 100644 --- a/tests/Doctrine/Tests/ORM/Functional/Locking/OptimisticTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/Locking/OptimisticTest.php @@ -5,6 +5,7 @@ namespace Doctrine\Tests\ORM\Functional\Locking; use DateTime; +use DateTimeImmutable; use Doctrine\DBAL\LockMode; use Doctrine\ORM\Annotation as ORM; use Doctrine\ORM\OptimisticLockException; @@ -26,6 +27,7 @@ protected function setUp() : void $this->em->getClassMetadata(OptimisticJoinedChild::class), $this->em->getClassMetadata(OptimisticStandard::class), $this->em->getClassMetadata(OptimisticTimestamp::class), + $this->em->getClassMetadata(OptimisticImmutableTimestamp::class), ] ); } catch (Exception $e) { @@ -204,6 +206,22 @@ public function testOptimisticTimestampSetsDefaultValue() : OptimisticTimestamp return $test; } + public function testOptimisticImmutableTimestampSetsDefaultValue() : OptimisticImmutableTimestamp + { + $test = new OptimisticImmutableTimestamp(); + + $test->name = 'Testing'; + + self::assertNull($test->version, 'Pre-Condition'); + + $this->em->persist($test); + $this->em->flush(); + + self::assertInstanceOf(DateTimeImmutable::class, $test->version); + + return $test; + } + /** * @depends testOptimisticTimestampSetsDefaultValue */ @@ -339,3 +357,22 @@ class OptimisticTimestamp /** @ORM\Version @ORM\Column(type="datetime") */ public $version; } + +/** + * @ORM\Entity + * @ORM\Table(name="optimistic_immutable_timestamp") + */ +class OptimisticImmutableTimestamp +{ + /** + * @ORM\Id @ORM\Column(type="integer") + * @ORM\GeneratedValue(strategy="AUTO") + */ + public $id; + + /** @ORM\Column(type="string", length=255) */ + public $name; + + /** @ORM\Version @ORM\Column(type="datetime_immutable") */ + public $version; +}