From 59aea220b61de2f1ec98686c9d3d47f085bc0448 Mon Sep 17 00:00:00 2001 From: Jeremy Mikola Date: Tue, 23 Feb 2016 21:34:56 +0000 Subject: [PATCH] Note best practices for BSON (un)serialization interfaces https://jira.mongodb.org/browse/PHPC-549 git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@338656 c90b9560-bf6c-de11-be94-00142212c4b1 --- reference/mongodb/bson/persistable.xml | 20 +++++++++++++++++- .../bson/serializable/bsonserialize.xml | 21 ++++++++++++++++--- .../bson/unserializable/bsonunserialize.xml | 4 ++++ 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/reference/mongodb/bson/persistable.xml b/reference/mongodb/bson/persistable.xml index 28c8f4d3ca..dc0dd2b566 100644 --- a/reference/mongodb/bson/persistable.xml +++ b/reference/mongodb/bson/persistable.xml @@ -12,8 +12,26 @@
&reftitle.intro; - + Classes may implement this interface to take advantage of automatic ODM + (object document mapping) behavior in the driver. During serialization, the + driver will inject a __pclass property containing the + PHP class name into the data returned by + MongoDB\BSON\Serializable::bsonSerialize. During + unserialization, the same __pclass property will then + be used to infer the PHP class (independent of any + type map configuration) + to be constructed before + MongoDB\BSON\Unserializable::bsonUnserialize is + invoked. See for additional + information. + + + Even if MongoDB\BSON\Serializable::bsonSerialize would + return a sequential array, injection of the __pclass + property will cause the object to be serialized as a BSON document. + +
diff --git a/reference/mongodb/bson/serializable/bsonserialize.xml b/reference/mongodb/bson/serializable/bsonserialize.xml index d6d009f48b..6a02c07874 100644 --- a/reference/mongodb/bson/serializable/bsonserialize.xml +++ b/reference/mongodb/bson/serializable/bsonserialize.xml @@ -25,6 +25,14 @@ document and sequential arrays (i.e. sequential, numeric indexes starting at 0) will be serialized as a BSON array. + + Users are encouraged to include an _id property (e.g. a + MongoDB\BSON\ObjectID initialized in your constructor) + when returning data for a BSON root document; otherwise, the driver or + database will need to generate a + MongoDB\BSON\ObjectID when inserting or upserting the + document, respectively. + @@ -51,9 +59,16 @@ class MyDocument implements MongoDB\BSON\Serializable { + private $id; + + function __construct() + { + $this->id = new MongoDB\BSON\ObjectID; + } + function bsonSerialize() { - return ['foo' => 'bar']; + return ['_id' => $this->id, 'foo' => 'bar']; } } @@ -63,10 +78,10 @@ echo MongoDB\BSON\toJSON($bson), "\n"; ?> ]]> - &example.outputs; + &example.outputs.similar; diff --git a/reference/mongodb/bson/unserializable/bsonunserialize.xml b/reference/mongodb/bson/unserializable/bsonunserialize.xml index 1e6feb4d37..fb87e693a6 100644 --- a/reference/mongodb/bson/unserializable/bsonunserialize.xml +++ b/reference/mongodb/bson/unserializable/bsonunserialize.xml @@ -17,6 +17,10 @@ Called during unserialization of the object from BSON. The properties of the BSON array or document will be passed to the method as an array. + + Remember to check for an _id property when handling data + from a BSON document. + This method acts as the