Skip to content

Commit 6881cdf

Browse files
authored
Merge pull request #12264 from doctrine/3.5.x-merge-up-into-3.6.x_9GolPzTd
Merge release 3.5.5 into 3.6.x
2 parents 9e5442a + dede2d7 commit 6881cdf

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

src/Tools/SchemaTool.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
use Doctrine\DBAL\Schema\AbstractAsset;
1010
use Doctrine\DBAL\Schema\AbstractSchemaManager;
1111
use Doctrine\DBAL\Schema\ComparatorConfig;
12+
use Doctrine\DBAL\Schema\DefaultExpression;
13+
use Doctrine\DBAL\Schema\DefaultExpression\CurrentDate;
14+
use Doctrine\DBAL\Schema\DefaultExpression\CurrentTime;
15+
use Doctrine\DBAL\Schema\DefaultExpression\CurrentTimestamp;
1216
use Doctrine\DBAL\Schema\ForeignKeyConstraintEditor;
1317
use Doctrine\DBAL\Schema\Index;
1418
use Doctrine\DBAL\Schema\Index\IndexedColumn;
@@ -18,6 +22,7 @@
1822
use Doctrine\DBAL\Schema\PrimaryKeyConstraint;
1923
use Doctrine\DBAL\Schema\Schema;
2024
use Doctrine\DBAL\Schema\Table;
25+
use Doctrine\DBAL\Types\Types;
2126
use Doctrine\ORM\EntityManagerInterface;
2227
use Doctrine\ORM\Mapping\AssociationMapping;
2328
use Doctrine\ORM\Mapping\ClassMetadata;
@@ -46,6 +51,7 @@
4651
use function current;
4752
use function implode;
4853
use function in_array;
54+
use function interface_exists;
4955
use function is_numeric;
5056
use function method_exists;
5157
use function preg_match;
@@ -489,6 +495,37 @@ private function gatherColumn(
489495
// the 'default' option can be overwritten here
490496
$options = $this->gatherColumnOptions($mapping) + $options;
491497

498+
if (isset($options['default']) && interface_exists(DefaultExpression::class)) {
499+
if (
500+
in_array($mapping->type, [
501+
Types::DATETIME_MUTABLE,
502+
Types::DATETIME_IMMUTABLE,
503+
Types::DATETIMETZ_MUTABLE,
504+
Types::DATETIMETZ_IMMUTABLE,
505+
], true)
506+
&& $options['default'] === $this->platform->getCurrentTimestampSQL()
507+
) {
508+
/** @phpstan-ignore class.notFound (if DefaultExpression exists, CurrentTimestamp exists as well) */
509+
$options['default'] = new CurrentTimestamp();
510+
}
511+
512+
if (
513+
in_array($mapping->type, [Types::TIME_MUTABLE, Types::TIME_IMMUTABLE], true)
514+
&& $options['default'] === $this->platform->getCurrentTimeSQL()
515+
) {
516+
/** @phpstan-ignore class.notFound (if DefaultExpression exists, CurrentTime exists as well) */
517+
$options['default'] = new CurrentTime();
518+
}
519+
520+
if (
521+
in_array($mapping->type, [Types::DATE_MUTABLE, Types::DATE_IMMUTABLE], true)
522+
&& $options['default'] === $this->platform->getCurrentDateSQL()
523+
) {
524+
/** @phpstan-ignore class.notFound (if DefaultExpression exists, CurrentDate exists as well) */
525+
$options['default'] = new CurrentDate();
526+
}
527+
}
528+
492529
if ($class->isIdGeneratorIdentity() && $class->getIdentifierFieldNames() === [$mapping->fieldName]) {
493530
$options['autoincrement'] = true;
494531
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\Tests\ORM\Functional;
6+
7+
use DateTime;
8+
use DateTimeImmutable;
9+
use Doctrine\Deprecations\PHPUnit\VerifyDeprecations;
10+
use Doctrine\ORM\Mapping as ORM;
11+
use Doctrine\Tests\OrmFunctionalTestCase;
12+
13+
class DefaultTimeExpressionTest extends OrmFunctionalTestCase
14+
{
15+
use VerifyDeprecations;
16+
17+
public function testUsingTimeRelatedDefaultExpressionCausesNoDbalDeprecation(): void
18+
{
19+
$this->expectNoDeprecationWithIdentifier('https://github.com/doctrine/dbal/pull/7195');
20+
21+
$this->createSchemaForModels(TimeEntity::class);
22+
}
23+
}
24+
25+
#[ORM\Entity]
26+
class TimeEntity
27+
{
28+
#[ORM\Id]
29+
#[ORM\Column]
30+
public int $id;
31+
32+
#[ORM\Column(options: ['default' => 'CURRENT_TIMESTAMP'])]
33+
public DateTime $createdAt;
34+
35+
#[ORM\Column(options: ['default' => 'CURRENT_TIMESTAMP'])]
36+
public DateTimeImmutable $createdAtImmutable;
37+
38+
#[ORM\Column(options: ['default' => 'CURRENT_TIME'])]
39+
public DateTime $createdTime;
40+
41+
#[ORM\Column(options: ['default' => 'CURRENT_DATE'])]
42+
public DateTime $createdDate;
43+
}

0 commit comments

Comments
 (0)