Strong Like Bull
Strong Like Bull

Reputation: 11297

Why are GridFS file name and bytes null?

I have successfully added a file to GRIDFS using the following:

 $asset = new Asset();
   $asset->setName('Image'.$count);
   $asset->setFile($uploadedFile->getPathname());
   $dm->persist($asset);
   $dm->flush();

I then try printing the file using the follows:

$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();

But nothing shows up. So I do:

var_dump($image);

and get the follows:

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"
}

Why is filename and bytes NULL?

Upvotes: 3

Views: 1234

Answers (1)

jmikola
jmikola

Reputation: 6922

If you look at the GridFSFile source, you'll see that $bytes is only used when overwriting the contents of a file. The $filename property is sometimes set during the getter, but is otherwise used when changing the filename in GridFS.

Based on the other values in your var_dump() output, it looks like there is certainly a file in GridFS (it has a chunk size, byte length, md5 hash, etc.). I would suggest debugging the GridFSFile::getBytes() method and ensuring that it's properly chaining to the getBytes() method on the internal MongoGridFSFile instance. Alternatively, you can try calling GridFSFile::getMongoGridFSFile() and working with the raw driver class directly. That would narrow this down to either a Doctrine or driver issue.

Concidentally, what version of the PECL driver are you using? GridFS has had some bugs in the past, and there have been some updates in recent versions (see: changelog).

Upvotes: 3

Related Questions