Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Easier way to return array as default #39

Closed
vlechemin opened this issue Oct 15, 2015 · 7 comments
Closed

Easier way to return array as default #39

vlechemin opened this issue Oct 15, 2015 · 7 comments

Comments

@vlechemin
Copy link

I'm trying to use this library in a project who uses the old driver.
But now the default behavior is to return objects from queries.
The only way I found was to make a wrapper around Collection and to specify for each operation (find, findOne, aggregate...) the type map.
Is there a better way to do this? Do you plan having default options for this kind of use case? (inherit from database/client, similar to read preference)
Thanks!

@jmikola
Copy link
Member

jmikola commented Oct 15, 2015

Thanks for the feedback! We've actually been tracking this in PHPLIB-74 and hope to have it resolved before the 1.0.0 release.

Supporting a type map option at the Client, Database, and Collection level is definitely feasible, and we'd also like to provide a class implementing ArrayAccess to use by default (instead of stdClass). We definitely don't want to encourage regular PHP arrays, since those are lossy (we have no way of knowing if [0 => 'foo'] is really an array or an object when converting back to BSON); however, you'd certainly be allowed to specify an array in your own type map.

@vlechemin
Copy link
Author

Thank you, that's great news. The class implementing ArrayAccess would be the perfect transition. Also, I'm not sure if it is a type map feature, but is it possible to allow a custom class for ObjectID to implement the JsonSerializable interface or add a public property to the class ObjectID (it used to be $id in the old driver) so the json_encode function does not return an empty object.

@bjori
Copy link
Contributor

bjori commented Oct 16, 2015

@vlechemin You can use BSON\toJSON() for that

    $oid = new MongoDB\BSON\ObjectID();
    $bson = MongoDB\BSON\fromPHP(array("oid" => $oid));
    $json = MongoDB\BSON\toJSON($bson);
    var_dump($json);
string(51) "{ "oid" : { "$oid" : "56213136bd21b96081103f11" } }"

I thought we had a ticket for going from PHP to (e)JSON but I cannot find it at the moment.
The intention was that you should be able to encode the BSON objects (such as ObjectID) using json_encode().

Its not going to make it into 1.0.0 though

@bjori
Copy link
Contributor

bjori commented Oct 16, 2015

@vlechemin
Copy link
Author

Thanks for the temporary solution and the ticket, I will be waiting for 1.1

@jmikola
Copy link
Member

jmikola commented Dec 28, 2015

On a related note, 1.0.0-beta2 of the library includes a new top-level typeMap option to centrally control BSON unserialization throughout the library.

@jmikola
Copy link
Member

jmikola commented Jan 6, 2016

#78 has now been merged (and PHPLIB-74 resolved), which means ArrayObject instances are being returned by default for BSON arrays and documents. We should have this out in a 1.0.0 release shortly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants