Skip to content

Commit d1b0ed3

Browse files
committed
null reference $id should throw DocumentNotFoundException
1 parent e8a7a0d commit d1b0ed3

File tree

2 files changed

+67
-5
lines changed

2 files changed

+67
-5
lines changed

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ public function refresh($id, $document)
410410
public function load($criteria, $document = null, array $hints = array(), $lockMode = 0, array $sort = null)
411411
{
412412
// TODO: remove this
413-
if (is_scalar($criteria) || $criteria instanceof \MongoId) {
413+
if ($criteria === null || is_scalar($criteria) || $criteria instanceof \MongoId) {
414414
$criteria = array('_id' => $criteria);
415415
}
416416

@@ -622,9 +622,6 @@ private function loadReferenceManyCollectionOwningSide(PersistentCollection $col
622622
$mongoId = $reference['$id'];
623623
}
624624
$id = $this->dm->getClassMetadata($className)->getPHPIdentifierValue($mongoId);
625-
if ( ! $id) {
626-
continue;
627-
}
628625

629626
// create a reference to the class and id
630627
$reference = $this->dm->getReference($className, $id);

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

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,4 +269,69 @@ public function testSortReferenceManyOwningSide()
269269
$this->assertEquals('Group 2', $groups[0]->getName());
270270
$this->assertEquals('Group 1', $groups[1]->getName());
271271
}
272-
}
272+
273+
/**
274+
* Fixes: Argument 1 passed to Doctrine\ODM\MongoDB\Persisters\DocumentPersister::prepareQueryOrNewObj() must be an array, null given
275+
*
276+
* @expectedException \Doctrine\ODM\MongoDB\DocumentNotFoundException
277+
* @expectedExceptionMessage The "Proxies\__CG__\Documents\Profile" document with identifier "" could not be found.
278+
*/
279+
public function testReferenceOneWithNullId()
280+
{
281+
$profile = new Profile();
282+
$user = new User();
283+
$user->setProfile($profile);
284+
285+
$this->dm->persist($profile);
286+
$this->dm->persist($user);
287+
$this->dm->flush();
288+
$this->dm->clear();
289+
290+
$collection = $this->dm->getDocumentCollection(get_class($user));
291+
292+
$collection->update(
293+
array('_id' => new \MongoId($user->getId())),
294+
array('$set' => array(
295+
'profile.$id' => null,
296+
))
297+
);
298+
299+
$user = $this->dm->find(get_class($user), $user->getId());
300+
$profile = $user->getProfile();
301+
$profile->__load();
302+
}
303+
304+
/**
305+
* Fixes: Argument 1 passed to Doctrine\ODM\MongoDB\Persisters\DocumentPersister::prepareQueryOrNewObj() must be an array, null given
306+
*
307+
* @expectedException \Doctrine\ODM\MongoDB\DocumentNotFoundException
308+
* @expectedExceptionMessage The "Proxies\__CG__\Documents\Group" document with identifier "" could not be found.
309+
*/
310+
public function testReferenceManyWithNullId()
311+
{
312+
$user = new User();
313+
$user->addGroup(new Group('Group'));
314+
315+
$this->dm->persist($user);
316+
$this->dm->flush();
317+
$this->dm->clear();
318+
319+
$collection = $this->dm->getDocumentCollection(get_class($user));
320+
321+
$collection->update(
322+
array('_id' => new \MongoId($user->getId())),
323+
array('$set' => array(
324+
'groups.0.$id' => null,
325+
))
326+
);
327+
328+
$user = $this->dm->find(get_class($user), $user->getId());
329+
$groups = $user->getGroups();
330+
331+
$this->assertEquals(1, count($groups));
332+
333+
foreach ($groups as $group) {
334+
$group->__load();
335+
}
336+
}
337+
}

0 commit comments

Comments
 (0)