Skip to content

Commit

Permalink
Fixes changeset being empty when datetime change is sub second (#2676)
Browse files Browse the repository at this point in the history
* Update UnitOfWork.php

* Add test (and remove fix to see failure)

* Add comment

* add fix

* phpcs & use fix

* fix typo

* Fix phpcs
  • Loading branch information
magnetik authored Sep 20, 2024
1 parent 8e0deb1 commit 3c9b1e8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
8 changes: 3 additions & 5 deletions lib/Doctrine/ODM/MongoDB/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
Expand Down
15 changes: 15 additions & 0 deletions tests/Doctrine/ODM/MongoDB/Tests/Functional/DateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 3c9b1e8

Please sign in to comment.