Browse Source

yii\mongodb\file\Collection::ensureMongoId() fixed to suppress exception on invalid _id format.

tags/2.0.0-beta
Paul Klimov 11 years ago
parent
commit
866ba85010
  1. 8
      extensions/mongodb/Collection.php
  2. 20
      tests/unit/extensions/mongodb/CollectionTest.php

8
extensions/mongodb/Collection.php

@ -733,7 +733,13 @@ class Collection extends Object
$rawId = (string)$rawId;
}
}
return new \MongoId($rawId);
try {
$mongoId = new \MongoId($rawId);
} catch (\MongoException $e) {
// invalid id format
$mongoId = $rawId;
}
return $mongoId;
}
/**

20
tests/unit/extensions/mongodb/CollectionTest.php

@ -419,4 +419,24 @@ class CollectionTest extends MongoDbTestCase
$this->assertNotEmpty($result);
$this->assertCount(2, $result);
}
public function testFindByNotObjectId()
{
$collection = $this->getConnection()->getCollection('customer');
$data = [
'name' => 'customer 1',
'address' => 'customer 1 address',
];
$id = $collection->insert($data);
$cursor = $collection->find(['_id' => (string)$id]);
$this->assertTrue($cursor instanceof \MongoCursor);
$row = $cursor->getNext();
$this->assertEquals($id, $row['_id']);
$cursor = $collection->find(['_id' => 'fake']);
$this->assertTrue($cursor instanceof \MongoCursor);
$this->assertEquals(0, $cursor->count());
}
}
Loading…
Cancel
Save