Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/Mapping/Event/Adapter/ODM.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Event\LifecycleEventArgs;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
use Doctrine\ODM\MongoDB\UnitOfWork;
use Gedmo\Exception\RuntimeException;
use Gedmo\Mapping\Event\AdapterInterface;

Expand All @@ -29,6 +30,8 @@ class ODM implements AdapterInterface

private ?DocumentManager $dm = null;

private static ?bool $useIntId = null;

public function __call($method, $args)
{
Deprecation::trigger(
Expand Down Expand Up @@ -150,12 +153,12 @@ public function getScheduledObjectDeletions($uow)

public function setOriginalObjectProperty($uow, $object, $property, $value)
{
$uow->setOriginalDocumentProperty(spl_object_hash($object), $property, $value);
$uow->setOriginalDocumentProperty($this->getOid($uow, $object), $property, $value);
}

public function clearObjectChangeSet($uow, $object)
{
$uow->clearDocumentChangeSet(spl_object_hash($object));
$uow->clearDocumentChangeSet($this->getOid($uow, $object));
}

/**
Expand All @@ -179,4 +182,21 @@ public function createLifecycleEventArgsInstance($document, $documentManager)

return new LifecycleEventArgs($document, $documentManager);
}

/**
* @return int|string dependent on the version of `doctrine/mongodb-odm` installed
*/
private function getOid(UnitOfWork $uow, object $object)
{
if (null === self::$useIntId) {
$refl = new \ReflectionClass($uow);
$method = $refl->getMethod('setOriginalDocumentProperty');
$oidArg = $method->getParameters()[0];

/** @phpstan-ignore-next-line method.NotFound All supported versions of `doctrine/mongodb-odm` have the first param typehinted */
self::$useIntId = 'int' === $oidArg->getType()->getName();
}

return true === self::$useIntId ? spl_object_id($object) : spl_object_hash($object);
}
}