Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fetch MongoGridFSFile after storage instead of manual construction

Manually constructing a MongoGridFSFile to save on GridFSFile causes problems if we end up calling getSize() on that object later. Fetch the actual object using the driver to ensure the internal state is sound.

See: 45016c7#commitcomment-2126151
  • Loading branch information...
commit 29f47eb3b969269264cd44b695fa5fec992f161c 1 parent 7f81f21
@jmikola jmikola authored
View
10 lib/Doctrine/MongoDB/GridFS.php
@@ -195,16 +195,22 @@ protected function doSave(array &$a, array $options = array())
*/
public function storeFile($file, array &$document, array $options = array())
{
- if (is_string($file)) {
+ if (!$file instanceof GridFSFile) {
$file = new GridFSFile($file);
}
+
if ($file->hasUnpersistedFile()) {
$id = $this->getMongoCollection()->storeFile($file->getFilename(), $document, $options);
} else {
$id = $this->getMongoCollection()->storeBytes($file->getBytes(), $document, $options);
}
+
$document = array_merge(array('_id' => $id), $document);
- $file->setMongoGridFSFile(new \MongoGridFSFile($this->getMongoCollection(), $document));
+ $gridFsFile = $this->getMongoCollection()->get($id);
+
+ // TODO: Consider throwing exception if file cannot be fetched
+ $file->setMongoGridFSFile($this->getMongoCollection()->get($id));
+
return $file;
}
View
15 tests/Doctrine/MongoDB/Tests/GridFSTest.php
@@ -38,14 +38,19 @@ public function testInsertFindOneAndWrite()
public function testStoreFile()
{
+ $document = array('foo' => 'bar');
+
$gridFS = $this->getGridFS();
+ $file = $gridFS->storeFile(__FILE__, $document);
+
+ $this->assertTrue(isset($document['_id']));
+ $this->assertEquals('bar', $document['foo']);
- $metadata = array(
- 'test' => 'file'
- );
- $file = $gridFS->storeFile(__DIR__.'/file.txt', $metadata);
$this->assertInstanceOf('Doctrine\MongoDB\GridFSFile', $file);
- $this->assertTrue(isset($metadata['_id']));
+ $this->assertFalse($file->isDirty());
+ $this->assertEquals(__FILE__, $file->getFilename());
+ $this->assertStringEqualsFile(__FILE__, $file->getBytes());
+ $this->assertEquals(filesize(__FILE__), $file->getSize());
}
public function testUpdate()
Please sign in to comment.
Something went wrong with that request. Please try again.