private function doPersist($document, array &$visited)
{
$oid = spl_object_hash($document);
if (isset($visited[$oid])) {
return;
// Prevent infinite recursion
}
$visited[$oid] = $document;
// Mark visited
$class = $this->dm->getClassMetadata(get_class($document));
$documentState = $this->getDocumentState($document, self::STATE_NEW);
switch ($documentState) {
case self::STATE_MANAGED:
// Nothing to do, except if policy is "deferred explicit"
if ($class->isChangeTrackingDeferredExplicit()) {
$this->scheduleForDirtyCheck($document);
}
break;
case self::STATE_NEW:
$this->persistNew($class, $document);
break;
case self::STATE_REMOVED:
// Document becomes managed again
unset($this->documentDeletions[$oid]);
$this->documentStates[$oid] = self::STATE_MANAGED;
break;
case self::STATE_DETACHED:
throw new \InvalidArgumentException('Behavior of persist() for a detached document is not yet defined.');
default:
throw MongoDBException::invalidDocumentState($documentState);
}
$this->cascadePersist($document, $visited);
}