PHP7 and the new Grid from mongo-php-library

418 views
Skip to first unread message

Момчил Божинов

unread,
May 21, 2016, 4:01:30 AM5/21/16
to mongodb-user
Hi,

I'm strugling with the PHP7 glue for MongoDb (https://github.com/mongodb/mongo-php-library/)

<?php

set_include_path(BASE_DIR."/MongoDB");
spl_autoload_register();

include("MongoDB/functions.php");

class MongoGrid
{
private $bucket;
function __construct($dbname = "TEST") {
$this->bucket = new \MongoDB\GridFS\Bucket(new \MongoDB\Driver\Manager("mongodb://localhost:27017"), $dbname);
}
public function find(array $args){
return $this->bucket->find($args);
}

function downloadToFile(array $args) {
        $handle = fopen($args['destination'], 'w+');
        $this->bucket->downloadToStreamByName($args['filename'], $handle);
        fclose($handle);
}

}

$grid = new MongoGrid("GridTest");
$grid->downloadToFile(['filename' => 'file.txt', 'destination' => 'file2.txt']);
$grid->find(['filename' => 'file.txt']);

?>

 My problem with this is that I have to a download and then a find to get any additional metadata that I have stored in there along with the file
It used to be done with a single query

Jeremy Mikola

unread,
May 23, 2016, 9:18:20 AM5/23/16
to mongod...@googlegroups.com
Since the library now returns a stream resource (per the driver specification), PHP requires that we use stream_get_meta_data() to access information about the stream. See the following annotated code example:

/* After opening a readable stream resource, stream_get_meta_data()
 * must be used to access "header/meta data" for the stream. */
$stream = $bucket->openDownloadStream($id);
$streamMetadata = stream_get_meta_data($stream);

/* wrapper_data will be an instance of the library's StreamWrapper
 * class, which has a public $context property. */
$streamContext = $metadata['wrapper_data']->context;

/* Since StreamWrapper::$context is also a resource, we need to
 * use stream_context_get_options() to access it as an array. */
$streamContextOptions = stream_context_get_options($context);

/* The library stores its metadata under a "gridfs" key. Readable
 * streams will have the "file" document within that array. */
var_dump($options['gridfs']['file']);


This is admittedly a very roundabout way of obtaining the fs.files document. It will certainly end up in the documentation and we will consider adding a utility function to shortcut this process later on.

As you know, the GridFS implementation for the library is currently unreleased and sitting in the master branch. We still have a bit of work to do on polishing this up and documenting the API before we can ship it. If you are using these APIs now, just keep in mind that they may be refactored or change before 1.1.0 is released.
 

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: https://docs.mongodb.org/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/76d63f3e-803b-4a22-949a-13a67690446f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Момчил Божинов

unread,
May 24, 2016, 4:06:33 AM5/24/16
to mongodb-user
Thank you Jeremy

I have a bunch of unit tests so changes to the APIs are welcome

Can you share the shipping date for the 1.1 release ?

Jeremy Mikola

unread,
Jun 3, 2016, 4:59:55 PM6/3/16
to mongod...@googlegroups.com
There is no set date. I am actively working on tidying up the GridFS component, but it has taken a backseat to work on the extension. If you'd like to follow progress, PHPLIB-114 and mongodb/mongo-php-library#189 would be the issues to watch.
 
To your earlier question about obtaining the files document, I noticed that we have a Bucket::getIdFromStream() method available to conveniently access the ID of the files document, given a stream resource. This uses the stream_get_meta_data() method I alluded to above. I will likely create a similar method to make access of the entire fs.files document more convenient.


Reply all
Reply to author
Forward
0 new messages