From 78b60f2c862aa126af32e6212ec9104a484e5d26 Mon Sep 17 00:00:00 2001 From: pepeh Date: Fri, 5 Nov 2021 12:59:43 +0200 Subject: [PATCH 1/5] Use array_diff_key to get diff for new or deleted documents (#2387) --- .../PersistentCollectionTrait.php | 30 +++++-------------- 1 file changed, 8 insertions(+), 22 deletions(-) diff --git a/lib/Doctrine/ODM/MongoDB/PersistentCollection/PersistentCollectionTrait.php b/lib/Doctrine/ODM/MongoDB/PersistentCollection/PersistentCollectionTrait.php index 29133d39b4..fcf3c6398a 100644 --- a/lib/Doctrine/ODM/MongoDB/PersistentCollection/PersistentCollectionTrait.php +++ b/lib/Doctrine/ODM/MongoDB/PersistentCollection/PersistentCollectionTrait.php @@ -255,18 +255,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_hash', $this->snapshot), $this->snapshot); + $newObjectsByOid = \array_combine(\array_map('spl_object_hash', $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} */ @@ -284,18 +277,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(); + $loadedObjectsByOid = \array_combine(\array_map('spl_object_hash', $this->snapshot), $this->snapshot); + $newObjectsByOid = \array_combine(\array_map('spl_object_hash', $coll), $coll); - 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} */ From 13359013ef5b7c016ee763fed399d26cdb8a425e Mon Sep 17 00:00:00 2001 From: pepeh Date: Fri, 5 Nov 2021 13:13:40 +0200 Subject: [PATCH 2/5] remove backslashes --- .../PersistentCollectionTrait.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/Doctrine/ODM/MongoDB/PersistentCollection/PersistentCollectionTrait.php b/lib/Doctrine/ODM/MongoDB/PersistentCollection/PersistentCollectionTrait.php index fcf3c6398a..7a03a67980 100644 --- a/lib/Doctrine/ODM/MongoDB/PersistentCollection/PersistentCollectionTrait.php +++ b/lib/Doctrine/ODM/MongoDB/PersistentCollection/PersistentCollectionTrait.php @@ -256,10 +256,10 @@ static function ($a, $b) { public function getDeletedDocuments() { $coll = $this->coll->toArray(); - $loadedObjectsByOid = \array_combine(\array_map('spl_object_hash', $this->snapshot), $this->snapshot); - $newObjectsByOid = \array_combine(\array_map('spl_object_hash', $coll), $coll); + $loadedObjectsByOid = array_combine(array_map('spl_object_hash', $this->snapshot), $this->snapshot); + $newObjectsByOid = array_combine(array_map('spl_object_hash', $coll), $coll); - return array_values(\array_diff_key($loadedObjectsByOid, $newObjectsByOid)); + return array_values(array_diff_key($loadedObjectsByOid, $newObjectsByOid)); } /** {@inheritdoc} */ @@ -278,10 +278,10 @@ static function ($a, $b) { public function getInsertedDocuments() { $coll = $this->coll->toArray(); - $loadedObjectsByOid = \array_combine(\array_map('spl_object_hash', $this->snapshot), $this->snapshot); - $newObjectsByOid = \array_combine(\array_map('spl_object_hash', $coll), $coll); + $loadedObjectsByOid = array_combine(array_map('spl_object_hash', $this->snapshot), $this->snapshot); + $newObjectsByOid = array_combine(array_map('spl_object_hash', $coll), $coll); - return array_values(\array_diff_key($newObjectsByOid, $loadedObjectsByOid)); + return array_values(array_diff_key($newObjectsByOid, $loadedObjectsByOid)); } /** {@inheritdoc} */ From f0301a2b1f568fd2a112d3d702efa952fdd0d0d8 Mon Sep 17 00:00:00 2001 From: pepeh Date: Fri, 5 Nov 2021 13:35:15 +0200 Subject: [PATCH 3/5] Fix code style --- .../PersistentCollectionTrait.php | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/Doctrine/ODM/MongoDB/PersistentCollection/PersistentCollectionTrait.php b/lib/Doctrine/ODM/MongoDB/PersistentCollection/PersistentCollectionTrait.php index 7a03a67980..d6f2cb177c 100644 --- a/lib/Doctrine/ODM/MongoDB/PersistentCollection/PersistentCollectionTrait.php +++ b/lib/Doctrine/ODM/MongoDB/PersistentCollection/PersistentCollectionTrait.php @@ -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. @@ -255,9 +256,9 @@ static function ($a, $b) { /** {@inheritdoc} */ public function getDeletedDocuments() { - $coll = $this->coll->toArray(); + $coll = $this->coll->toArray(); $loadedObjectsByOid = array_combine(array_map('spl_object_hash', $this->snapshot), $this->snapshot); - $newObjectsByOid = array_combine(array_map('spl_object_hash', $coll), $coll); + $newObjectsByOid = array_combine(array_map('spl_object_hash', $coll), $coll); return array_values(array_diff_key($loadedObjectsByOid, $newObjectsByOid)); } @@ -277,9 +278,9 @@ static function ($a, $b) { /** {@inheritdoc} */ public function getInsertedDocuments() { - $coll = $this->coll->toArray(); + $coll = $this->coll->toArray(); $loadedObjectsByOid = array_combine(array_map('spl_object_hash', $this->snapshot), $this->snapshot); - $newObjectsByOid = array_combine(array_map('spl_object_hash', $coll), $coll); + $newObjectsByOid = array_combine(array_map('spl_object_hash', $coll), $coll); return array_values(array_diff_key($newObjectsByOid, $loadedObjectsByOid)); } From e01fe301d6fbda274001cfeaec60b2d642db2643 Mon Sep 17 00:00:00 2001 From: pepeh Date: Fri, 5 Nov 2021 13:53:01 +0200 Subject: [PATCH 4/5] Return is_object check --- .../PersistentCollectionTrait.php | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/Doctrine/ODM/MongoDB/PersistentCollection/PersistentCollectionTrait.php b/lib/Doctrine/ODM/MongoDB/PersistentCollection/PersistentCollectionTrait.php index d6f2cb177c..c7ae975de0 100644 --- a/lib/Doctrine/ODM/MongoDB/PersistentCollection/PersistentCollectionTrait.php +++ b/lib/Doctrine/ODM/MongoDB/PersistentCollection/PersistentCollectionTrait.php @@ -19,6 +19,7 @@ use function count; use function get_class; use function is_object; +use function spl_object_hash; /** * Trait with methods needed to implement PersistentCollectionInterface. @@ -256,9 +257,13 @@ static function ($a, $b) { /** {@inheritdoc} */ public function getDeletedDocuments() { + $callback = static function ($val) { + return is_object($val) ? spl_object_hash($val) : $val; + }; + $coll = $this->coll->toArray(); - $loadedObjectsByOid = array_combine(array_map('spl_object_hash', $this->snapshot), $this->snapshot); - $newObjectsByOid = array_combine(array_map('spl_object_hash', $coll), $coll); + $loadedObjectsByOid = array_combine(array_map($callback, $this->snapshot), $this->snapshot); + $newObjectsByOid = array_combine(array_map($callback, $coll), $coll); return array_values(array_diff_key($loadedObjectsByOid, $newObjectsByOid)); } @@ -278,9 +283,13 @@ static function ($a, $b) { /** {@inheritdoc} */ public function getInsertedDocuments() { + $callback = static function ($val) { + return is_object($val) ? spl_object_hash($val) : $val; + }; + $coll = $this->coll->toArray(); - $loadedObjectsByOid = array_combine(array_map('spl_object_hash', $this->snapshot), $this->snapshot); - $newObjectsByOid = array_combine(array_map('spl_object_hash', $coll), $coll); + $newObjectsByOid = array_combine(array_map($callback, $coll), $coll); + $loadedObjectsByOid = array_combine(array_map($callback, $this->snapshot), $this->snapshot); return array_values(array_diff_key($newObjectsByOid, $loadedObjectsByOid)); } From aeaa4a38d7c06648f1f500860a99de4b94ad43f7 Mon Sep 17 00:00:00 2001 From: pepeh Date: Thu, 11 Nov 2021 19:18:07 +0200 Subject: [PATCH 5/5] Use spl_object_id instead of of spl_object_hash --- .../PersistentCollectionTrait.php | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/lib/Doctrine/ODM/MongoDB/PersistentCollection/PersistentCollectionTrait.php b/lib/Doctrine/ODM/MongoDB/PersistentCollection/PersistentCollectionTrait.php index c7ae975de0..e7cd7563ea 100644 --- a/lib/Doctrine/ODM/MongoDB/PersistentCollection/PersistentCollectionTrait.php +++ b/lib/Doctrine/ODM/MongoDB/PersistentCollection/PersistentCollectionTrait.php @@ -19,7 +19,6 @@ use function count; use function get_class; use function is_object; -use function spl_object_hash; /** * Trait with methods needed to implement PersistentCollectionInterface. @@ -257,13 +256,9 @@ static function ($a, $b) { /** {@inheritdoc} */ public function getDeletedDocuments() { - $callback = static function ($val) { - return is_object($val) ? spl_object_hash($val) : $val; - }; - $coll = $this->coll->toArray(); - $loadedObjectsByOid = array_combine(array_map($callback, $this->snapshot), $this->snapshot); - $newObjectsByOid = array_combine(array_map($callback, $coll), $coll); + $loadedObjectsByOid = array_combine(array_map('spl_object_id', $this->snapshot), $this->snapshot); + $newObjectsByOid = array_combine(array_map('spl_object_id', $coll), $coll); return array_values(array_diff_key($loadedObjectsByOid, $newObjectsByOid)); } @@ -283,13 +278,9 @@ static function ($a, $b) { /** {@inheritdoc} */ public function getInsertedDocuments() { - $callback = static function ($val) { - return is_object($val) ? spl_object_hash($val) : $val; - }; - $coll = $this->coll->toArray(); - $newObjectsByOid = array_combine(array_map($callback, $coll), $coll); - $loadedObjectsByOid = array_combine(array_map($callback, $this->snapshot), $this->snapshot); + $newObjectsByOid = array_combine(array_map('spl_object_id', $coll), $coll); + $loadedObjectsByOid = array_combine(array_map('spl_object_id', $this->snapshot), $this->snapshot); return array_values(array_diff_key($newObjectsByOid, $loadedObjectsByOid)); }