@@ -21,8 +21,8 @@ By default, the |php-library| returns BSON documents as
21
21
:phpclass:`MongoDB\\Model\\BSONDocument` objects and BSON arrays as
22
22
:phpclass:`MongoDB\\Model\\BSONArray` objects, respectively.
23
23
24
- BSON Classes
25
- ------------
24
+ Classes
25
+ -------
26
26
27
27
.. phpclass:: MongoDB\\Model\\BSONArray
28
28
@@ -49,213 +49,3 @@ BSON Classes
49
49
class. During BSON and JSON serialization, instances of this class will
50
50
serialize as a document type (:php:`object casting
51
51
<types.type-juggling#language.types.typecasting>` is used internally).
52
-
53
- .. _php-type-map:
54
-
55
- Type Maps
56
- ---------
57
-
58
- Most methods that read data from MongoDB support a ``typeMap`` option, which
59
- allows control over how BSON is converted to PHP. Additionally,
60
- the :phpclass:`MongoDB\\Client`, :phpclass:`MongoDB\\Database`, and
61
- :phpclass:`MongoDB\\Collection` classes accept a ``typeMap`` option, which can
62
- be used to specify a default type map to apply to any supporting methods and
63
- selected classes (e.g. :phpmethod:`MongoDB\\Client::selectDatabase()`).
64
-
65
- The :phpclass:`MongoDB\\Client`, :phpclass:`MongoDB\\Database`, and
66
- :phpclass:`MongoDB\\Collection` classes use the following type map by
67
- default:
68
-
69
- .. code-block:: php
70
-
71
- [
72
- 'array' => 'MongoDB\Model\BSONArray',
73
- 'document' => 'MongoDB\Model\BSONDocument',
74
- 'root' => 'MongoDB\Model\BSONDocument',
75
- ]
76
-
77
- The type map above will convert BSON documents and arrays to
78
- :phpclass:`MongoDB\\Model\\BSONDocument` and
79
- :phpclass:`MongoDB\\Model\\BSONArray` objects, respectively. The ``root`` and
80
- ``document`` keys are used to distinguish the top-level BSON document from
81
- embedded documents, respectively.
82
-
83
- A type map may specify any class that implements
84
- :php:`MongoDB\\BSON\\Unserializable <mongodb-bson-unserializable>` as well as
85
- ``"array"``, ``"stdClass``", and ``"object"`` (``"stdClass``" and ``"object"``
86
- are aliases of one another).
87
-
88
- .. seealso:: :php:`Deserialization from BSON <manual/en/mongodb.persistence.deserialization.php>` in the PHP manual
89
-
90
- ``Persistable`` Classes
91
- -----------------------
92
-
93
- The driver's :php:`persistence specification <mongodb.persistence>` outlines how
94
- classes implementing its :php:`MongoDB\\BSON\\Persistable
95
- <mongodb-bson-persistable>` interface are serialized to and deserialized from
96
- BSON. The :php:`Persistable <mongodb-bson-persistable>` interface is analogous
97
- to PHP's :php:`Serializable interface <class.serializable>`.
98
-
99
- The driver automatically handles serialization and deserialization for classes
100
- implementing the :php:`Persistable <mongodb-bson-persistable>` interface without
101
- requiring the use of the ``typeMap`` option. This is done by encoding the name
102
- of the PHP class in a special property within the BSON document.
103
-
104
- .. note::
105
-
106
- When deserializing a PHP variable from BSON, the encoded class name of a
107
- :php:`Persistable <mongodb-bson-persistable>` object will override any class
108
- specified in the type map, but it will not override ``"array"`` and
109
- ``"stdClass"`` or ``"object"``. This is discussed in the
110
- :php:`persistence specification <mongodb.persistence>` but it bears
111
- repeating.
112
-
113
- Consider the following class definition:
114
-
115
- .. code-block:: php
116
-
117
- <?php
118
-
119
- class Person implements MongoDB\BSON\Persistable
120
- {
121
- private $id;
122
- private $name;
123
- private $createdAt;
124
-
125
- public function __construct($name)
126
- {
127
- $this->id = new MongoDB\BSON\ObjectId;
128
- $this->name = (string) $name;
129
- $this->createdAt = new MongoDB\BSON\UTCDateTime;
130
- }
131
-
132
- function bsonSerialize()
133
- {
134
- return [
135
- '_id' => $this->id,
136
- 'name' => $this->name,
137
- 'createdAt' => $this->createdAt,
138
- ];
139
- }
140
-
141
- function bsonUnserialize(array $data)
142
- {
143
- $this->id = $data['_id'];
144
- $this->name = $data['name'];
145
- $this->createdAt = $data['createdAt'];
146
- }
147
- }
148
-
149
- The following example constructs a ``Person`` object, inserts it into the
150
- database, and reads it back as an object of the same type:
151
-
152
- .. code-block:: php
153
-
154
- <?php
155
-
156
- $collection = (new MongoDB\Client)->test->persons;
157
-
158
- $result = $collection->insertOne(new Person('Bob'));
159
-
160
- $person = $collection->findOne(['_id' => $result->getInsertedId()]);
161
-
162
- var_dump($person);
163
-
164
- The output would then resemble:
165
-
166
- .. code-block:: none
167
-
168
- object(Person)#18 (3) {
169
- ["id":"Person":private]=>
170
- object(MongoDB\BSON\ObjectId)#15 (1) {
171
- ["oid"]=>
172
- string(24) "56fad2c36118fd2e9820cfc1"
173
- }
174
- ["name":"Person":private]=>
175
- string(3) "Bob"
176
- ["createdAt":"Person":private]=>
177
- object(MongoDB\BSON\UTCDateTime)#17 (1) {
178
- ["milliseconds"]=>
179
- int(1459278531218)
180
- }
181
- }
182
-
183
- The same document in the MongoDB shell might display as:
184
-
185
- .. code-block:: js
186
-
187
- {
188
- "_id" : ObjectId("56fad2c36118fd2e9820cfc1"),
189
- "__pclass" : BinData(128,"UGVyc29u"),
190
- "name" : "Bob",
191
- "createdAt" : ISODate("2016-03-29T19:08:51.218Z")
192
- }
193
-
194
- .. note::
195
-
196
- :php:`MongoDB\\BSON\\Persistable <mongodb-bson-persistable>` may only be used
197
- for root and embedded BSON documents. It may not be used for BSON arrays.
198
-
199
- Emulating the Legacy Driver
200
- ---------------------------
201
-
202
- The legacy ``mongo`` extension returned both BSON documents and arrays as PHP
203
- arrays. While PHP arrays are convenient to work with, this behavior was
204
- problematic:
205
-
206
- - Different BSON types could deserialize to the same PHP value (e.g.
207
- ``{"0": "foo"}`` and ``["foo"]``), which made it impossible to infer the
208
- original BSON type.
209
-
210
- - Numerically-indexed PHP arrays would be serialized as BSON documents if there
211
- was a gap in their key sequence. Such gaps were easily caused by unsetting a
212
- key to remove an element and forgetting to numerically reindex the array.
213
-
214
- The |php-library|'s :phpclass:`BSONDocument <MongoDB\\Model\\BSONDocument>` and
215
- :phpclass:`BSONArray <MongoDB\\Model\\BSONArray>` classes address these concerns
216
- by preserving the BSON type information during serialization and
217
- deserialization; however, some users may still prefer the legacy behavior. If
218
- desired, you can use the ``typeMap`` option to have the library return
219
- everything as a PHP array:
220
-
221
- .. code-block:: php
222
-
223
- <?php
224
-
225
- $client = new MongoDB\Client(
226
- 'mongodb://127.0.0.1/',
227
- [],
228
- [
229
- 'typeMap' => [
230
- 'array' => 'array',
231
- 'document' => 'array',
232
- 'root' => 'array',
233
- ],
234
- ]
235
- );
236
-
237
- $document = $client->test->zips->findOne(['_id' => '94301']);
238
-
239
- var_dump($document);
240
-
241
- The above example would output something similar to:
242
-
243
- .. code-block:: php
244
-
245
- array(5) {
246
- ["_id"]=>
247
- string(5) "94301"
248
- ["city"]=>
249
- string(9) "PALO ALTO"
250
- ["loc"]=>
251
- array(2) {
252
- [0]=>
253
- float(-122.149685)
254
- [1]=>
255
- float(37.444324)
256
- }
257
- ["pop"]=>
258
- int(15965)
259
- ["state"]=>
260
- string(2) "CA"
261
- }
0 commit comments