我已使用以下方法成功地将文件添加到 GRIDFS:
$asset = new Asset();
$asset->setName('Image'.$count);
$asset->setFile($uploadedFile->getPathname());
$dm->persist($asset);
$dm->flush();
然后我尝试使用以下方法打印文件:
$dm = $this->get('doctrine.odm.mongodb.document_manager');
$image = $dm->createQueryBuilder('MyBundle:Asset')
->field('id')->equals($imageID)
->getQuery()
->getSingleResult();
header('Content-type: image/png;');
echo $image->getFile()->getBytes();
但什么也没有出现。所以我这样做:
var_dump($image);
并获得以下信息:
object(Main\MyBundle\Document\Asset)#429 (7) {
["id":protected]=>
string(24) "50330286c7e24c7019000004"
["name":protected]=>
string(6) "Image2"
["file":protected]=>
object(Doctrine\MongoDB\GridFSFile)#427 (4) {
["mongoGridFSFile":"Doctrine\MongoDB\GridFSFile":private]=>
object(MongoGridFSFile)#430 (3) {
["file"]=>
array(7) {
["_id"]=>
object(MongoId)#431 (1) {
["$id"]=>
string(24) "50330286c7e24c7019000004"
}
["name"]=>
string(6) "Image2"
["filename"]=>
string(14) "/tmp/phpQ1LCIC"
["uploadDate"]=>
object(MongoDate)#432 (2) {
["sec"]=>
int(1345520262)
["usec"]=>
int(510000)
}
["length"]=>
float(194992)
["chunkSize"]=>
float(262144)
["md5"]=>
string(32) "5bbc9ede74f50f93a3f7d1f7babe3170"
}
["gridfs":protected]=>
object(MongoGridFS)#437 (5) {
["w"]=>
int(1)
["wtimeout"]=>
int(10000)
["chunks"]=>
object(MongoCollection)#438 (2) {
["w"]=>
int(1)
["wtimeout"]=>
int(10000)
}
["filesName":protected]=>
string(12) "assets.files"
["chunksName":protected]=>
string(13) "assets.chunks"
}
["flags"]=>
int(0)
}
["filename":"Doctrine\MongoDB\GridFSFile":private]=>
NULL
["bytes":"Doctrine\MongoDB\GridFSFile":private]=>
NULL
["isDirty":"Doctrine\MongoDB\GridFSFile":private]=>
bool(false)
}
["uploadDate":protected]=>
string(21) "0.51000000 1345520262"
["length":protected]=>
string(6) "194992"
["chunkSize":protected]=>
string(6) "262144"
["md5":protected]=>
string(32) "5bbc9ede74f50f93a3f7d1f7babe3170"
}
为什么文件名和字节为 NULL?