Skip to content

Commit

Permalink
Merge pull request #2388 from pepeh/2387-fix-collection-documents-diff
Browse files Browse the repository at this point in the history
Use array_diff_key to get diff for new or deleted documents
  • Loading branch information
IonBazan authored Nov 12, 2021
2 parents 3deac8a + aeaa4a3 commit 5259b02
Showing 1 changed file with 11 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@
use Doctrine\ODM\MongoDB\UnitOfWork;
use Doctrine\ODM\MongoDB\Utility\CollectionHelper;

use function array_udiff;
use function array_combine;
use function array_diff_key;
use function array_map;
use function array_udiff_assoc;
use function array_values;
use function count;
use function get_class;
use function is_object;
use function spl_object_hash;

/**
* Trait with methods needed to implement PersistentCollectionInterface.
Expand Down Expand Up @@ -255,18 +256,11 @@ static function ($a, $b) {
/** {@inheritdoc} */
public function getDeletedDocuments()
{
$compare = static function ($a, $b) {
$compareA = is_object($a) ? spl_object_hash($a) : $a;
$compareb = is_object($b) ? spl_object_hash($b) : $b;
$coll = $this->coll->toArray();
$loadedObjectsByOid = array_combine(array_map('spl_object_id', $this->snapshot), $this->snapshot);
$newObjectsByOid = array_combine(array_map('spl_object_id', $coll), $coll);

return $compareA === $compareb ? 0 : ($compareA > $compareb ? 1 : -1);
};

return array_values(array_udiff(
$this->snapshot,
$this->coll->toArray(),
$compare
));
return array_values(array_diff_key($loadedObjectsByOid, $newObjectsByOid));
}

/** {@inheritdoc} */
Expand All @@ -284,18 +278,11 @@ static function ($a, $b) {
/** {@inheritdoc} */
public function getInsertedDocuments()
{
$compare = static function ($a, $b) {
$compareA = is_object($a) ? spl_object_hash($a) : $a;
$compareb = is_object($b) ? spl_object_hash($b) : $b;
$coll = $this->coll->toArray();
$newObjectsByOid = array_combine(array_map('spl_object_id', $coll), $coll);
$loadedObjectsByOid = array_combine(array_map('spl_object_id', $this->snapshot), $this->snapshot);

return $compareA === $compareb ? 0 : ($compareA > $compareb ? 1 : -1);
};

return array_values(array_udiff(
$this->coll->toArray(),
$this->snapshot,
$compare
));
return array_values(array_diff_key($newObjectsByOid, $loadedObjectsByOid));
}

/** {@inheritdoc} */
Expand Down

0 comments on commit 5259b02

Please sign in to comment.