Skip to content

Commit

Permalink
Merge pull request #7558 from nico-incubiq/nico-incubiq-3.0-datetimei…
Browse files Browse the repository at this point in the history
…mmutable-version

Allow all datetime types to be used as version
  • Loading branch information
Ocramius committed Jan 5, 2019
2 parents d00453d + 3225163 commit 22bf778
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/Doctrine/ORM/Mapping/ClassMetadata.php
Expand Up @@ -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;
Expand Down
37 changes: 37 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/Locking/OptimisticTest.php
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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;
}

0 comments on commit 22bf778

Please sign in to comment.