2012-08-24 16 views
3

我已经成功将一个文件中使用到GridFS的以下内容:为什么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?

回答

3

如果您查看GridFSFile源代码,您将看到$bytes仅在覆盖文件内容时使用。 $filename属性有时在getter中设置,但在GridFS中更改文件名时使用该属性。

根据您的var_dump()输出中的其他值,它看起来像GridFS中肯定存在一个文件(它具有块大小,字节长度,md5散列等)。我建议调试GridFSFile::getBytes()方法,并确保它正确链接到内部MongoGridFSFile实例上的getBytes()方法。或者,您可以尝试拨打GridFSFile::getMongoGridFSFile()并直接使用原始驱动程序类。这可能会缩小到教条或驱动问题。

巧合的是,您使用的是哪种版本的PECL驱动程序? GridFS在过去有一些bug,并且在最近的版本中有一些更新(参见:changelog)。

+0

Web服务器 Web服务器Apache的\t/2.2.22 PHP版本\t PHP 5.3.15 PHP扩展\t蒙戈/ 1.2.12 – jini

+0

我已经调试了BTH包括GridFSFile :: getMongoGridFSFile(代码)和我都确实有一个字节大小,但是当涉及到getBytes时,我无法取回任何东西。这是非常混乱的。 – jini

+0

您是否尝试过直接连接到Mongo并使用[MongoGridFS :: get()](http://php.net/manual/en/mongogridfs.get.php)与ObjectId单独的CLI脚本? – jmikola

相关问题