From 4016d6ba4bced0186f8f5e50f0faffa18fbbb3c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Paris?= Date: Thu, 13 Nov 2025 23:09:45 +0100 Subject: [PATCH] Deprecate string default expressions Right now, the ORM handles the conversion of strings that happen to be default expressions for date, time and datetime columns into the corresponding value objects. Let us allow users to specify these value objects directly, and deprecate relying on the aforementioned conversion. --- UPGRADE.md | 89 ++++++++++ docs/en/reference/basic-mapping.rst | 16 ++ .../reference/basic-mapping/DefaultValues.php | 5 + .../basic-mapping/default-values.xml | 7 + doctrine-mapping.xsd | 16 +- phpstan-baseline.neon | 4 +- src/Mapping/Driver/XmlDriver.php | 47 +++++- src/Tools/SchemaTool.php | 31 ++++ .../Functional/DefaultTimeExpressionTest.php | 159 +++++++++++++++++- .../DefaultTimeExpressionXmlTest.php | 101 +++++++++++ ...ORM.Functional.XmlLegacyTimeEntity.dcm.xml | 36 ++++ ...Tests.ORM.Functional.XmlTimeEntity.dcm.xml | 44 +++++ 12 files changed, 543 insertions(+), 12 deletions(-) create mode 100644 tests/Tests/ORM/Functional/DefaultTimeExpressionXmlTest.php create mode 100644 tests/Tests/ORM/Mapping/xml/Doctrine.Tests.ORM.Functional.XmlLegacyTimeEntity.dcm.xml create mode 100644 tests/Tests/ORM/Mapping/xml/Doctrine.Tests.ORM.Functional.XmlTimeEntity.dcm.xml diff --git a/UPGRADE.md b/UPGRADE.md index 5c4808604b0..06190a641cc 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -29,6 +29,95 @@ and directly start using native lazy objects. # Upgrade to 3.6 +## Deprecate using string expression for default values in mappings + +Using a string expression for default values in field mappings is deprecated. +Use `Doctrine\DBAL\Schema\DefaultExpression` instances instead. + +Here is how to address this deprecation when mapping entities using PHP attributes: + +```diff + use DateTime; ++use Doctrine\DBAL\Schema\DefaultExpression\CurrentDate; ++use Doctrine\DBAL\Schema\DefaultExpression\CurrentTime; ++use Doctrine\DBAL\Schema\DefaultExpression\CurrentTimestamp; + use Doctrine\ORM\Mapping as ORM; + + #[ORM\Entity] + final class TimeEntity + { + #[ORM\Id] + #[ORM\Column] + public int $id; + +- #[ORM\Column(options: ['default' => 'CURRENT_TIMESTAMP'], insertable: false, updatable: false)] ++ #[ORM\Column(options: ['default' => new CurrentTimestamp()], insertable: false, updatable: false)] + public DateTime $createdAt; + +- #[ORM\Column(options: ['default' => 'CURRENT_TIME'], insertable: false, updatable: false)] ++ #[ORM\Column(options: ['default' => new CurrentTime()], insertable: false, updatable: false)] + public DateTime $createdTime; + +- #[ORM\Column(options: ['default' => 'CURRENT_DATE'], insertable: false, updatable: false)] ++ #[ORM\Column(options: ['default' => new CurrentDate()], insertable: false, updatable: false)] + public DateTime $createdDate; + } +``` + +Here is how to do the same when mapping entities using XML: + +```diff + + + + + + + + + + + +- ++