Skip to content

Commit 3c9b1e8

Browse files
authored
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
1 parent 8e0deb1 commit 3c9b1e8

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

lib/Doctrine/ODM/MongoDB/UnitOfWork.php

+3-5
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
use Doctrine\Persistence\NotifyPropertyChanged;
2525
use Doctrine\Persistence\PropertyChangedListener;
2626
use InvalidArgumentException;
27-
use MongoDB\BSON\UTCDateTime;
2827
use MongoDB\Driver\Exception\RuntimeException;
2928
use MongoDB\Driver\Session;
3029
use MongoDB\Driver\WriteConcern;
@@ -835,10 +834,9 @@ private function computeOrRecomputeChangeSet(ClassMetadata $class, object $docum
835834
$dbOrgValue = $dateType->convertToDatabaseValue($orgValue);
836835
$dbActualValue = $dateType->convertToDatabaseValue($actualValue);
837836

838-
$orgTimestamp = $dbOrgValue instanceof UTCDateTime ? $dbOrgValue->toDateTime()->getTimestamp() : null;
839-
$actualTimestamp = $dbActualValue instanceof UTCDateTime ? $dbActualValue->toDateTime()->getTimestamp() : null;
840-
841-
if ($orgTimestamp === $actualTimestamp) {
837+
// We rely on loose comparison to compare every field (including microseconds)
838+
// phpcs:ignore SlevomatCodingStandard.Operators.DisallowEqualOperators.DisallowedEqualOperator
839+
if ($dbOrgValue == $dbActualValue) {
842840
continue;
843841
}
844842
}

tests/Doctrine/ODM/MongoDB/Tests/Functional/DateTest.php

+15
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,21 @@ public static function provideEquivalentDates(): array
6969
];
7070
}
7171

72+
public function testDateInstanceChangeWhenValueDifferenceIsSubSecond(): void
73+
{
74+
$user = new User();
75+
$user->setCreatedAt(new UTCDateTime(100000000000));
76+
$this->dm->persist($user);
77+
$this->dm->flush();
78+
$this->dm->clear();
79+
80+
$user = $this->dm->getRepository($user::class)->findOneBy([]);
81+
$user->setCreatedAt(new UTCDateTime(100000000123));
82+
$this->dm->getUnitOfWork()->computeChangeSets();
83+
$changeset = $this->dm->getUnitOfWork()->getDocumentChangeSet($user);
84+
self::assertNotEmpty($changeset);
85+
}
86+
7287
public function testDateInstanceValueChangeDoesCauseUpdateIfValueIsTheSame(): void
7388
{
7489
$user = new User();

0 commit comments

Comments
 (0)