为什么GridFS文件名和字节为空


Why are GridFS file name and bytes null?

我已经使用以下方法成功地将文件添加到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?

如果查看GridFSFile源代码,就会发现$bytes仅在覆盖文件内容时使用。$filename属性有时在getter期间设置,但在GridFS中更改文件名时会使用。

根据var_dump()输出中的其他值,看起来GridFS中肯定有一个文件(它有块大小、字节长度、md5哈希等)。我建议调试GridFSFile::getBytes()方法,并确保它正确链接到内部MongoGridFSFile实例上的getBytes()方法。或者,您可以尝试调用GridFSFile::getMongoGridFSFile()并直接使用原始驱动程序类。这将把范围缩小到一个条令或驾驶员问题。

顺便问一下,您使用的是什么版本的PECL驱动程序?GridFS在过去有一些错误,在最近的版本中也有一些更新(请参阅:changelog)。