Skip to content

Commit 16c2324

Browse files
committed
Do not use keys in DocumentPersister calls to Cursor::toArray()
Allowing iterator_to_array() to use document "_id" fields as array keys is problematic when documents have non-scalar keys (e.g. embedded objects or MongoBinData, which seems to always return "<Mongo Binary Data>" as its string value). See doctrine#854 for more context. This change also reverts ec69eaa from doctrine#401. Technically, inverse side relationships should be read-only, so there should be no reason for a set strategy. The previous behavior was also inconsistent with owning side relationships, which used keys from the actual array/object containing the references (not the "_id" fields of the referenced documents).
1 parent e29f6d2 commit 16c2324

File tree

1 file changed

+4
-9
lines changed

1 file changed

+4
-9
lines changed

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,7 @@ private function loadReferenceManyCollectionOwningSide(PersistentCollection $col
692692
if ( ! empty($hints[Query::HINT_READ_PREFERENCE])) {
693693
$cursor->setReadPreference($hints[Query::HINT_READ_PREFERENCE], $hints[Query::HINT_READ_PREFERENCE_TAGS]);
694694
}
695-
$documents = $cursor->toArray();
695+
$documents = $cursor->toArray(false);
696696
foreach ($documents as $documentData) {
697697
$document = $this->uow->getById($documentData['_id'], $class);
698698
$data = $this->hydratorFactory->hydrate($document, $documentData);
@@ -708,14 +708,9 @@ private function loadReferenceManyCollectionOwningSide(PersistentCollection $col
708708
private function loadReferenceManyCollectionInverseSide(PersistentCollection $collection)
709709
{
710710
$query = $this->createReferenceManyInverseSideQuery($collection);
711-
$documents = $query->execute()->toArray();
712-
$mapping = $collection->getMapping();
711+
$documents = $query->execute()->toArray(false);
713712
foreach ($documents as $key => $document) {
714-
if ($mapping['strategy'] === 'set') {
715-
$collection->set($key, $document);
716-
} else {
717-
$collection->add($document);
718-
}
713+
$collection->add($document);
719714
}
720715
}
721716

@@ -764,7 +759,7 @@ public function createReferenceManyInverseSideQuery(PersistentCollection $collec
764759
private function loadReferenceManyWithRepositoryMethod(PersistentCollection $collection)
765760
{
766761
$cursor = $this->createReferenceManyWithRepositoryMethodCursor($collection);
767-
$documents = $cursor->toArray();
762+
$documents = $cursor->toArray(false);
768763
foreach ($documents as $document) {
769764
$collection->add($document);
770765
}

0 commit comments

Comments
 (0)