From 3c9b1e8668343408cb70c9c9cd724b834d95438a Mon Sep 17 00:00:00 2001 From: Baptiste Lafontaine Date: Fri, 20 Sep 2024 14:31:14 +0200 Subject: [PATCH] Fixes changeset being empty when datetime change is sub second (#2676) * Update UnitOfWork.php * Add test (and remove fix to see failure) * Add comment * add fix * phpcs & use fix * fix typo * Fix phpcs --- lib/Doctrine/ODM/MongoDB/UnitOfWork.php | 8 +++----- .../ODM/MongoDB/Tests/Functional/DateTest.php | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/lib/Doctrine/ODM/MongoDB/UnitOfWork.php b/lib/Doctrine/ODM/MongoDB/UnitOfWork.php index 71f5f925f..eb9591f99 100644 --- a/lib/Doctrine/ODM/MongoDB/UnitOfWork.php +++ b/lib/Doctrine/ODM/MongoDB/UnitOfWork.php @@ -24,7 +24,6 @@ use Doctrine\Persistence\NotifyPropertyChanged; use Doctrine\Persistence\PropertyChangedListener; use InvalidArgumentException; -use MongoDB\BSON\UTCDateTime; use MongoDB\Driver\Exception\RuntimeException; use MongoDB\Driver\Session; use MongoDB\Driver\WriteConcern; @@ -835,10 +834,9 @@ private function computeOrRecomputeChangeSet(ClassMetadata $class, object $docum $dbOrgValue = $dateType->convertToDatabaseValue($orgValue); $dbActualValue = $dateType->convertToDatabaseValue($actualValue); - $orgTimestamp = $dbOrgValue instanceof UTCDateTime ? $dbOrgValue->toDateTime()->getTimestamp() : null; - $actualTimestamp = $dbActualValue instanceof UTCDateTime ? $dbActualValue->toDateTime()->getTimestamp() : null; - - if ($orgTimestamp === $actualTimestamp) { + // We rely on loose comparison to compare every field (including microseconds) + // phpcs:ignore SlevomatCodingStandard.Operators.DisallowEqualOperators.DisallowedEqualOperator + if ($dbOrgValue == $dbActualValue) { continue; } } diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Functional/DateTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Functional/DateTest.php index 76fecd68e..35aa83b83 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Functional/DateTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Functional/DateTest.php @@ -69,6 +69,21 @@ public static function provideEquivalentDates(): array ]; } + public function testDateInstanceChangeWhenValueDifferenceIsSubSecond(): void + { + $user = new User(); + $user->setCreatedAt(new UTCDateTime(100000000000)); + $this->dm->persist($user); + $this->dm->flush(); + $this->dm->clear(); + + $user = $this->dm->getRepository($user::class)->findOneBy([]); + $user->setCreatedAt(new UTCDateTime(100000000123)); + $this->dm->getUnitOfWork()->computeChangeSets(); + $changeset = $this->dm->getUnitOfWork()->getDocumentChangeSet($user); + self::assertNotEmpty($changeset); + } + public function testDateInstanceValueChangeDoesCauseUpdateIfValueIsTheSame(): void { $user = new User();