Skip to content

Commit 5259b02

Browse files
authored
Merge pull request #2388 from pepeh/2387-fix-collection-documents-diff
Use array_diff_key to get diff for new or deleted documents
2 parents 3deac8a + aeaa4a3 commit 5259b02

File tree

1 file changed

+11
-24
lines changed

1 file changed

+11
-24
lines changed

lib/Doctrine/ODM/MongoDB/PersistentCollection/PersistentCollectionTrait.php

+11-24
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@
1111
use Doctrine\ODM\MongoDB\UnitOfWork;
1212
use Doctrine\ODM\MongoDB\Utility\CollectionHelper;
1313

14-
use function array_udiff;
14+
use function array_combine;
15+
use function array_diff_key;
16+
use function array_map;
1517
use function array_udiff_assoc;
1618
use function array_values;
1719
use function count;
1820
use function get_class;
1921
use function is_object;
20-
use function spl_object_hash;
2122

2223
/**
2324
* Trait with methods needed to implement PersistentCollectionInterface.
@@ -255,18 +256,11 @@ static function ($a, $b) {
255256
/** {@inheritdoc} */
256257
public function getDeletedDocuments()
257258
{
258-
$compare = static function ($a, $b) {
259-
$compareA = is_object($a) ? spl_object_hash($a) : $a;
260-
$compareb = is_object($b) ? spl_object_hash($b) : $b;
259+
$coll = $this->coll->toArray();
260+
$loadedObjectsByOid = array_combine(array_map('spl_object_id', $this->snapshot), $this->snapshot);
261+
$newObjectsByOid = array_combine(array_map('spl_object_id', $coll), $coll);
261262

262-
return $compareA === $compareb ? 0 : ($compareA > $compareb ? 1 : -1);
263-
};
264-
265-
return array_values(array_udiff(
266-
$this->snapshot,
267-
$this->coll->toArray(),
268-
$compare
269-
));
263+
return array_values(array_diff_key($loadedObjectsByOid, $newObjectsByOid));
270264
}
271265

272266
/** {@inheritdoc} */
@@ -284,18 +278,11 @@ static function ($a, $b) {
284278
/** {@inheritdoc} */
285279
public function getInsertedDocuments()
286280
{
287-
$compare = static function ($a, $b) {
288-
$compareA = is_object($a) ? spl_object_hash($a) : $a;
289-
$compareb = is_object($b) ? spl_object_hash($b) : $b;
281+
$coll = $this->coll->toArray();
282+
$newObjectsByOid = array_combine(array_map('spl_object_id', $coll), $coll);
283+
$loadedObjectsByOid = array_combine(array_map('spl_object_id', $this->snapshot), $this->snapshot);
290284

291-
return $compareA === $compareb ? 0 : ($compareA > $compareb ? 1 : -1);
292-
};
293-
294-
return array_values(array_udiff(
295-
$this->coll->toArray(),
296-
$this->snapshot,
297-
$compare
298-
));
285+
return array_values(array_diff_key($newObjectsByOid, $loadedObjectsByOid));
299286
}
300287

301288
/** {@inheritdoc} */

0 commit comments

Comments
 (0)