Skip to content

Commit

Permalink
Deprecation and forward compat for flush
Browse files Browse the repository at this point in the history
  • Loading branch information
malarzm committed Jan 26, 2020
1 parent 7e8f58b commit e253a63
Showing 1 changed file with 35 additions and 5 deletions.
40 changes: 35 additions & 5 deletions lib/Doctrine/ODM/MongoDB/DocumentManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -531,17 +531,33 @@ public function getRepository($documentName)
* This effectively synchronizes the in-memory state of managed objects with the
* database.
*
* @param object $document
* @param object|array $documentOrOptions
* @param array $options Array of options to be used with batchInsert(), update() and remove()
* @throws \InvalidArgumentException
*/
public function flush($document = null, array $options = array())
public function flush($documentOrOptions = null, array $options = array())
{
if (null !== $document && ! is_object($document) && ! is_array($document)) {
throw new \InvalidArgumentException(gettype($document));
if (func_num_args() === 1 && is_array($documentOrOptions) && ! $this->arrayContainsDocuments($documentOrOptions)) {
$options = $documentOrOptions;
$documentOrOptions = null;
}
if (null !== $documentOrOptions && ! is_object($documentOrOptions) && ! is_array($documentOrOptions)) {
throw new \InvalidArgumentException(gettype($documentOrOptions));
}
if ($documentOrOptions !== null) {
@trigger_error(
'Flushing selected documents has been deprecated and will be removed in doctrine/mongodb-odm 2.0.',
E_USER_DEPRECATED
);
}
if (func_num_args() === 2) {
@trigger_error(
'$options will become a first argument in doctrine/mongodb-odm 2.0, you may pass it as first now.',
E_USER_DEPRECATED
);
}
$this->errorIfClosed();
$this->unitOfWork->commit($document, $options);
$this->unitOfWork->commit($documentOrOptions, $options);
}

/**
Expand Down Expand Up @@ -842,4 +858,18 @@ public function getFilterCollection()

return $this->filterCollection;
}

private function arrayContainsDocuments(array $documentOrOptions)
{
foreach ($documentOrOptions as $documentPerhaps) {
if (! is_object($documentPerhaps)) {
return false;
}
if (! $this->getMetadataFactory()->isTransient(get_class($documentPerhaps))) {
return true;
}
}

return false;
}
}

0 comments on commit e253a63

Please sign in to comment.