Skip to content

Commit 3ffc2ae

Browse files
authored
Merge pull request #1688 from alcaeus/fix-pushall-mongodb-3.6
Remove usage of pushAll operator in CollectionPersister
2 parents 858d656 + 93eb1b1 commit 3ffc2ae

File tree

5 files changed

+43
-24
lines changed

5 files changed

+43
-24
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ matrix:
2424
include:
2525
- php: 5.6
2626
env: DRIVER_VERSION="1.6.7" COMPOSER_FLAGS="--prefer-lowest" SERVER_VERSION="3.2" KEY_ID="EA312927"
27+
- php: 7.2
28+
env: SERVER_VERSION="3.6" KEY_ID="2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5"
2729

2830
before_install:
2931
- sudo apt-key adv --keyserver ${KEY_SERVER} --recv ${KEY_ID}

docs/en/reference/introduction.rst

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -284,24 +284,32 @@ this:
284284
[changes] => 2
285285
)
286286
287-
[$pushAll] => Array
287+
[$push] => Array
288288
(
289289
[notes] => Array
290290
(
291-
[0] => Gave user 100k a year raise
291+
[$each] => Array
292+
(
293+
[0] => Gave user 100k a year raise
294+
)
295+
292296
)
293-
297+
294298
[projects] => Array
295299
(
296-
[0] => Array
300+
[$each] => Array
297301
(
298-
[$ref] => projects
299-
[$id] => 4c0310718ead0e767e030000
300-
[$db] => my_db
302+
[0] => Array
303+
(
304+
[$ref] => projects
305+
[$id] => 4c0310718ead0e767e030000
306+
[$db] => my_db
307+
)
308+
301309
)
302-
310+
303311
)
304-
312+
305313
)
306314
307315
[$set] => Array

docs/en/reference/storage-strategies.rst

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ stored as a BSON array.
5555
pushAll
5656
-------
5757

58-
The ``pushAll`` strategy uses MongoDB's `$pushAll`_ operator to insert
59-
elements into the array. MongoDB does not allow elements to be added and removed
60-
from an array in a single operation, so this strategy relies on multiple update
61-
queries to remove and insert elements (in that order).
58+
The ``pushAll`` strategy uses MongoDB's `$push`_ operator in combination with
59+
`$each`_ to insert elements into the array. MongoDB does not allow elements to
60+
be added and removed from an array in a single operation, so this strategy
61+
relies on multiple update queries to remove and insert elements (in that order).
6262

6363
.. _atomic_set:
6464

@@ -92,6 +92,7 @@ BSON array.
9292

9393
.. _`$addToSet`: https://docs.mongodb.com/manual/reference/operator/update/addToSet/
9494
.. _`$inc`: https://docs.mongodb.com/manual/reference/operator/update/inc/
95-
.. _`$pushAll`: https://docs.mongodb.com/manual/reference/operator/update/pushAll/
95+
.. _`$push`: https://docs.mongodb.com/manual/reference/operator/update/push/
96+
.. _`$each`: https://docs.mongodb.com/manual/reference/operator/update/each/
9697
.. _`$set`: https://docs.mongodb.com/manual/reference/operator/update/set/
9798
.. _`$unset`: https://docs.mongodb.com/manual/reference/operator/update/unset/

lib/Doctrine/ODM/MongoDB/Persisters/CollectionPersister.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -200,21 +200,29 @@ private function insertElements(PersistentCollectionInterface $coll, array $opti
200200
}
201201

202202
$mapping = $coll->getMapping();
203-
list($propertyPath, $parent) = $this->getPathAndParent($coll);
204203

205-
$pb = $this->pb;
204+
switch ($mapping['strategy']) {
205+
case ClassMetadataInfo::STORAGE_STRATEGY_PUSH_ALL:
206+
$operator = 'push';
207+
break;
208+
209+
case ClassMetadataInfo::STORAGE_STRATEGY_ADD_TO_SET:
210+
$operator = 'addToSet';
211+
break;
212+
213+
default:
214+
throw new \LogicException("Invalid strategy {$mapping['strategy']} given for insertElements");
215+
}
216+
217+
list($propertyPath, $parent) = $this->getPathAndParent($coll);
206218

207219
$callback = isset($mapping['embedded'])
208-
? function($v) use ($pb, $mapping) { return $pb->prepareEmbeddedDocumentValue($mapping, $v); }
209-
: function($v) use ($pb, $mapping) { return $pb->prepareReferencedDocumentValue($mapping, $v); };
220+
? function($v) use ($mapping) { return $this->pb->prepareEmbeddedDocumentValue($mapping, $v); }
221+
: function($v) use ($mapping) { return $this->pb->prepareReferencedDocumentValue($mapping, $v); };
210222

211223
$value = array_values(array_map($callback, $insertDiff));
212224

213-
if ($mapping['strategy'] === ClassMetadataInfo::STORAGE_STRATEGY_ADD_TO_SET) {
214-
$value = array('$each' => $value);
215-
}
216-
217-
$query = array('$' . $mapping['strategy'] => array($propertyPath => $value));
225+
$query = ['$' . $operator => [$propertyPath => ['$each' => $value]]];
218226

219227
$this->executeQuery($parent, $query, $options);
220228
}

tests/Doctrine/ODM/MongoDB/Tests/Functional/FilesTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function testFileReferences()
8888
$this->dm->flush();
8989

9090
$check = $this->dm->getDocumentCollection(get_class($image))->findOne();
91-
$this->assertArrayNotHasKey('$pushAll', $check);
91+
$this->assertArrayNotHasKey('$push', $check);
9292
$this->assertArrayHasKey('profiles', $check);
9393
$this->assertCount(1, $check['profiles']);
9494
$this->assertEquals($profile->getProfileId(), (string) $check['profiles'][0]['$id']);

0 commit comments

Comments
 (0)