1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992
|
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<chapter xml:id="mongodb.architecture" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<titleabbrev>Driver Architecture and Internals</titleabbrev>
<title>Explains the driver architecture, and special features</title>
<section xml:id="mongodb.overview">
<titleabbrev>Architecture</titleabbrev>
<title>Architecture Overview</title>
<para>
This article explains how all the different components of the PHP driver fit
together, from base system libraries, through the extension, and to the PHP
libraries on top.
</para>
<para>
<mediaobject>
<alt>
MongoDB PHP Driver Architecture Diagram. The lowest level of the driver is
our system libraries: libmongoc, libbson, and libmongocrypt. The middle
level is the MongoDB PHP extension. The upper level is PHP userland and
includes the MongoDB PHP library and higher-level packages such as
framework integrations and applications.
</alt>
<imageobject>
<imagedata fileref="en/reference/mongodb/images/driver_arch.svg" width="625" depth="450" format="SVG" />
</imageobject>
</mediaobject>
</para>
<para>
At the top of this stack sits a
<link xlink:href="&url.mongodb.library;">PHP library</link>,
which is distributed as a
<link xlink:href="https://packagist.org/packages/mongodb/mongodb">Composer package</link>.
This library provides an API consistent with other MongoDB
<link xlink:href="&url.mongodb.drivers;">drivers</link>
and implements various cross-driver
<link xlink:href="&url.mongodb.specs;">specifications</link>.
While the extension can be used directly, the library has minimal overhead and
should be a common dependency for most applications built with MongoDB.
</para>
<para>
Sitting below that library is a PHP extension, which is distributed through
<link xlink:href="&url.pecl.package;mongodb">PECL</link>.
The extension forms the glue between PHP and our system libraries
(<link xlink:href="&url.mongodb.libmongoc;">libmongoc</link>,
<link xlink:href="&url.mongodb.libbson;">libbson</link>, and
<link xlink:href="&url.mongodb.libmongocrypt;">libmongocrypt</link>).
Its public API provides only the most essential functionality:
<simplelist>
<member>Connection management</member>
<member>BSON encoding and decoding</member>
<member>Object document serialization (to support ODM libraries)</member>
<member>Executing commands, queries, and write operations</member>
<member>Handling cursors for command and query results</member>
</simplelist>
</para>
<para>
<table>
<title>Driver Source Code and JIRA Projects</title>
<tgroup cols="3">
<thead>
<row>
<entry>Project</entry>
<entry>GitHub</entry>
<entry>JIRA</entry>
</row>
</thead>
<tbody>
<row>
<entry>PHP Library</entry>
<entry><link xlink:href="&url.mongodb.github.phplib;">mongodb/mongo-php-library</link></entry>
<entry><link xlink:href="&url.mongodb.jira.phplib;">PHPLIB</link></entry>
</row>
<row>
<entry>PHP Extension</entry>
<entry><link xlink:href="&url.mongodb.github.phpc;">mongodb/mongo-php-driver</link></entry>
<entry><link xlink:href="&url.mongodb.jira.phpc;">PHPC</link></entry>
</row>
</tbody>
</tgroup>
</table>
</para>
</section>
<section xml:id="mongodb.connection-handling">
<titleabbrev>Connections</titleabbrev>
<title>Connection handling and persistence</title>
&mongodb.note.forking;
<section>
<title>Connection and topology persistence (PHP version since 1.2.0)</title>
<para>
All versions of the extension since 1.2.0 persist the
<link xlink:href="&url.mongodb.libmongoc;">libmongoc</link> client object in
the PHP worker process, which allows it to re-use database connections,
authentication states, <emphasis>and</emphasis> topology information across
multiple requests.
</para>
<para>
When <methodname>MongoDB\Driver\Manager::__construct</methodname> is
invoked, a hash is created from its arguments (i.e. URI string and array
options). The extension will attempt to find a previously persisted
<link xlink:href="&url.mongodb.libmongoc;">libmongoc</link> client object for
that hash. If an existing client cannot be found for the hash, a new client
will be created and persisted for future use. This behavior can be disabled
via the <literal>"disableClientPersistence"</literal> driver option.
</para>
<para>
Each client contains its own database connections and a view of the server
topology (e.g. standalone, replica set, shard cluster). By persisting the
client between PHP requests, the extension is able to re-use established
database connections and remove the need for
<link xlink:href="&url.mongodb.sdam;">discovering the server topology</link>
on each request.
</para>
<para>
Consider the following example:
</para>
<programlisting role="php">
<![CDATA[
<?php
$managers = [
new MongoDB\Driver\Manager('mongodb://127.0.0.1'),
new MongoDB\Driver\Manager('mongodb://127.0.0.1'),
new MongoDB\Driver\Manager('mongodb://127.0.0.1:27017'),
new MongoDB\Driver\Manager('mongodb://rs1.example.com,rs2.example.com/', ['replicaSet' => 'myReplicaSet']),
];
foreach ($managers as $manager) {
$manager->executeCommand('test', new MongoDB\Driver\Command(['ping' => 1]));
}
?>
]]>
</programlisting>
<para>
The first two Manager objects will share the same
<link xlink:href="&url.mongodb.libmongoc;">libmongoc</link> client since
their constructor arguments are identical. The third and fourth objects will
each use their own client. In total, three clients will be created and the
PHP worker executing this script will open two connections to
<literal>127.0.0.1</literal> and one connection to each of
<literal>rs1.example.com</literal> and <literal>rs2.example.com</literal>.
If the extension discovers additional members of the replica set after
issuing <literal>hello</literal> commands, it will open additional
connections to those servers as well.
</para>
<para>
If the same worker executes the script again in a second request, the three
clients will be re-used and no new connections will be made. Depending on
how long ago the previous request was served, the extension may need to
issue additional <literal>hello</literal> commands to update its view of the
topologies.
</para>
</section>
<section>
<title>Socket persistence (PHP versions before 1.2.0)</title>
<para>
Versions of the extension before 1.2.0 utilize PHP's Streams API for
database connections, using an API within
<link xlink:href="&url.mongodb.libmongoc;">libmongoc</link> to designate
custom handlers for socket communication; however, a new libmongoc client is
created for each <classname>MongoDB\Driver\Manager</classname>. As a result,
the extension persists individual database connections but not
authentication state or topology information. This means that the extension
needs to issue commands at the start of each request to authenticate and
<link xlink:href="&url.mongodb.sdam;">discover the server topology</link>.
</para>
<para>
Database connections are persisted by a hash derived from the server's
host, port, and the URI string used to construct the
<classname>MongoDB\Driver\Manager</classname>. The constructor's array
options are not included in this hash.
</para>
<note>
<simpara>
Versions of the extension >= 1.1.8 and < 1.2.0 do not persist sockets
for SSL connections. See
<link xlink:href="&url.mongodb.jira.phpc;-720">PHPC-720</link> for
additional information.
</simpara>
</note>
<para>
Despite its shortcomings with persisting SSL connections when and topology
information, this version of the extension supports all
<link linkend="context.ssl">SSL context options</link> since it uses
PHP's Streams API.
</para>
</section>
</section>
<section xml:id="mongodb.persistence">
<titleabbrev>Persisting Data</titleabbrev>
<title>Serialization and deserialization of PHP variables into MongoDB</title>
<para>
This document discusses how compound structures (i.e. documents, arrays, and
objects) are converted between BSON and PHP values.
</para>
<section xml:id="mongodb.persistence.serialization">
<title>Serialization to BSON</title>
<section>
<title>Arrays</title>
<para>
If an array is a <emphasis>packed array</emphasis> — i.e. empty array or
the keys start at 0 and are sequential without gaps: <emphasis>BSON
array</emphasis>.
</para>
<para>
If the array is not packed — i.e. having associative (string) keys, the
keys don't start at 0, or when there are gaps:: <emphasis>BSON
object</emphasis>
</para>
<para>
A top-level (root) document, <emphasis>always</emphasis> serializes as a
BSON document.
</para>
<section>
<title>Examples</title>
<para>
These serialize as a BSON array:
</para>
<programlisting role="text">
<![CDATA[
[ 8, 5, 2, 3 ] => [ 8, 5, 2, 3 ]
[ 0 => 4, 1 => 9 ] => [ 4, 9 ]
]]>
</programlisting>
<para>
These serialize as a BSON document:
</para>
<programlisting role="text">
<![CDATA[
[ 0 => 1, 2 => 8, 3 => 12 ] => { "0" : 1, "2" : 8, "3" : 12 }
[ "foo" => 42 ] => { "foo" : 42 }
[ 1 => 9, 0 => 10 ] => { "1" : 9, "0" : 10 }
]]>
</programlisting>
<para>
Note that the five examples are <emphasis>extracts</emphasis> of a full
document, and represent only <emphasis>one</emphasis> value inside a
document.
</para>
</section>
</section>
<section>
<title>Objects</title>
<para>
If an object is of the <classname>stdClass</classname> class, serialize
as a <emphasis>BSON document</emphasis>.
</para>
<para>
If an object is a supported class that implements
<interfacename>MongoDB\BSON\Type</interfacename>, then use the BSON
serialization logic for that specific type.
<interfacename>MongoDB\BSON\Type</interfacename> instances (excluding
<interfacename>MongoDB\BSON\Serializable</interfacename> may only be
serialized as a document field value. Attempting to serialize such an
object as a root document will throw a
<classname>MongoDB\Driver\Exception\UnexpectedValueException</classname>
</para>
<para>
If an object is of an unknown class implementing the
<interfacename>MongoDB\BSON\Type</interfacename> interface, then throw a
<classname>MongoDB\Driver\Exception\UnexpectedValueException</classname>
</para>
<para>
If an object is of any other class, without implementing any special
interface, serialize as a <emphasis>BSON document</emphasis>. Keep only
<emphasis>public</emphasis> properties, and ignore
<emphasis>protected</emphasis> and <emphasis>private</emphasis>
properties.
</para>
<para>
If an object is of a class that implements the
<interfacename>MongoDB\BSON\Serializable</interfacename> interface, call
<methodname>MongoDB\BSON\Serializable::bsonSerialize</methodname> and use
the returned array or <classname>stdClass</classname> to serialize as a
BSON document or array. The BSON type will be determined by the following:
</para>
<para>
<orderedlist>
<listitem>
<para>Root documents must be serialized as a BSON
document.
</para>
</listitem>
<listitem>
<para>
<interfacename>MongoDB\BSON\Persistable</interfacename> objects must be
serialized as a BSON document.
</para>
</listitem>
<listitem>
<para>
If <methodname>MongoDB\BSON\Serializable::bsonSerialize</methodname>
returns a packed array, serialize as a BSON array.
</para>
</listitem>
<listitem>
<para>
If <methodname>MongoDB\BSON\Serializable::bsonSerialize</methodname>
returns a non-packed array or <classname>stdClass</classname>,
serialize as a BSON document.
</para>
</listitem>
<listitem>
<para>
If <methodname>MongoDB\BSON\Serializable::bsonSerialize</methodname>
did not return an array or <classname>stdClass</classname>, throw an
<classname>MongoDB\Driver\Exception\UnexpectedValueException</classname>
exception.
</para>
</listitem>
</orderedlist>
</para>
<para>
If an object is of a class that implements the
<interfacename>MongoDB\BSON\Persistable</interfacename> interface (which
implies <interfacename>MongoDB\BSON\Serializable</interfacename>), obtain
the properties in a similar way as in the previous paragraphs, but
<emphasis>also</emphasis> add an additional property
<property>__pclass</property> as a Binary value, with subtype
<literal>0x80</literal> and data bearing the fully qualified class name
of the object that is being serialized.
</para>
<para>
The <property>__pclass</property> property is added to the array or
object returned by
<methodname>MongoDB\BSON\Serializable::bsonSerialize</methodname>, which
means it will overwrite any <property>__pclass</property> key/property in
the <methodname>MongoDB\BSON\Serializable::bsonSerialize</methodname>
return value. If you want to avoid this behaviour and set your own
<property>__pclass</property> value, you must <emphasis>not</emphasis>
implement <interfacename>MongoDB\BSON\Persistable</interfacename> and
should instead implement
<interfacename>MongoDB\BSON\Serializable</interfacename> directly.
</para>
<section>
<title>Examples</title>
<programlisting role="php">
<![CDATA[
<?php
class stdClass
{
public $foo = 42;
} // => {"foo": 42}
class MyClass
{
public $foo = 42;
protected $prot = 'wine';
private $fpr = 'cheese';
} // => {"foo": 42}
class AnotherClass1 implements MongoDB\BSON\Serializable
{
public $foo = 42;
protected $prot = 'wine';
private $fpr = 'cheese';
public function bsonSerialize(): array
{
return ['foo' => $this->foo, 'prot' => $this->prot];
}
} // => {"foo": 42, "prot": "wine"}
class AnotherClass2 implements MongoDB\BSON\Serializable
{
public $foo = 42;
public function bsonSerialize(): self
{
return $this;
}
} // => MongoDB\Driver\Exception\UnexpectedValueException("bsonSerialize() did not return an array or stdClass")
class AnotherClass3 implements MongoDB\BSON\Serializable
{
private $elements = ['foo', 'bar'];
public function bsonSerialize(): array
{
return $this->elements;
}
} // => {"0": "foo", "1": "bar"}
/**
* Nesting Serializable classes
*/
class AnotherClass4 implements MongoDB\BSON\Serializable
{
private $elements = [0 => 'foo', 2 => 'bar'];
public function bsonSerialize(): array
{
return $this->elements;
}
} // => {"0": "foo", "2": "bar"}
class ContainerClass1 implements MongoDB\BSON\Serializable
{
public $things;
public function __construct()
{
$this->things = new AnotherClass4();
}
function bsonSerialize(): array
{
return ['things' => $this->things];
}
} // => {"things": {"0": "foo", "2": "bar"}}
class AnotherClass5 implements MongoDB\BSON\Serializable
{
private $elements = [0 => 'foo', 2 => 'bar'];
public function bsonSerialize(): array
{
return array_values($this->elements);
}
} // => {"0": "foo", "1": "bar"} as a root class
["foo", "bar"] as a nested value
class ContainerClass2 implements MongoDB\BSON\Serializable
{
public $things;
public function __construct()
{
$this->things = new AnotherClass5();
}
public function bsonSerialize(): array
{
return ['things' => $this->things];
}
} // => {"things": ["foo", "bar"]}
class AnotherClass6 implements MongoDB\BSON\Serializable
{
private $elements = ['foo', 'bar'];
function bsonSerialize(): object
{
return (object) $this->elements;
}
} // => {"0": "foo", "1": "bar"}
class ContainerClass3 implements MongoDB\BSON\Serializable
{
public $things;
public function __construct()
{
$this->things = new AnotherClass6();
}
public function bsonSerialize(): array
{
return ['things' => $this->things];
}
} // => {"things": {"0": "foo", "1": "bar"}}
class UpperClass implements MongoDB\BSON\Persistable
{
public $foo = 42;
protected $prot = 'wine';
private $fpr = 'cheese';
private $data;
public function bsonUnserialize(array $data): void
{
$this->data = $data;
}
public function bsonSerialize(): array
{
return ['foo' => $this->foo, 'prot' => $this->prot];
}
} // => {"foo": 42, "prot": "wine", "__pclass": {"$type": "80", "$binary": "VXBwZXJDbGFzcw=="}}
?>
]]>
</programlisting>
</section>
</section>
</section>
<section xml:id="mongodb.persistence.deserialization">
<title>Deserialization from BSON</title>
&mongodb.warning.duplicate-keys;
<para>
The legacy <code>mongo</code> extension deserialized
both BSON documents and arrays as PHP arrays. While PHP arrays are
convenient to work with, this behavior was problematic because different
BSON types could deserialize to the same PHP value (e.g.
<literal>{"0": "foo"}</literal> and <literal>["foo"]</literal>) and make it
impossible to infer the original BSON type. By default, the
<code>mongodb</code> extension addresses this concern by ensuring that BSON
arrays and documents are converted to PHP arrays and objects, respectively.
</para>
<para>
For compound types, there are three data types:
</para>
<para>
<variablelist>
<varlistentry>
<term>root</term>
<listitem>
<para>
refers to the top-level BSON document <emphasis>only</emphasis>
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>document</term>
<listitem>
<para>
refers to embedded BSON documents <emphasis>only</emphasis>
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>array</term>
<listitem>
<para>
refers to a BSON array
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
<para>
Besides the three collective types, it is also possible to configure
specific fields in your document to map to the data types mentioned below.
As an example, the following type map allows you to
map each embedded document within an <literal>"addresses"</literal> array to
an <classname>Address</classname> class <emphasis>and</emphasis> each
<literal>"city"</literal> field within those embedded address documents to
a <classname>City</classname> class:
<programlisting role="text">
<![CDATA[
[
'fieldPaths' => [
'addresses.$' => 'MyProject\Address',
'addresses.$.city' => 'MyProject\City',
],
]
]]>
</programlisting>
</para>
<para>
Each of those three data types, as well as the field specific mappings,
can be mapped against different PHP types. The possible mapping values
are:
</para>
<para>
<variablelist>
<varlistentry>
<term><emphasis>not set</emphasis> or <type>NULL</type> (default)</term>
<listitem>
<para>
<itemizedlist>
<listitem>
<para>
A BSON array will be deserialized as a PHP <type>array</type>.
</para>
</listitem>
<listitem>
<para>
A BSON document (root or embedded) without a
<property>__pclass</property> property
<footnote xml:id="mongodb.pclass">
<para>
A __pclass property is only deemed to exist if
there exists a property with that name, and it is a Binary value,
and the sub-type of the Binary value is 0x80. If any of these three
conditions is not met, the __pclass property does not exist and
should be treated as any other normal property.
</para>
</footnote>
becomes a PHP <classname>stdClass</classname> object, with each
BSON document key set as a public <classname>stdClass</classname>
property.
</para>
</listitem>
<listitem>
<para>
A BSON document (root or embedded) with a
<property>__pclass</property> property <footnoteref linkend="mongodb.pclass"/> becomes a PHP object of
the class name as defined by the <property>__pclass</property>
property.
</para>
<para>
If the named class implements the
<interfacename>MongoDB\BSON\Persistable</interfacename> interface,
then the properties of the BSON document, including the
<property>__pclass</property> property, are sent as an associative
array to the
<methodname>MongoDB\BSON\Unserializable::bsonUnserialize</methodname>
function to initialise the object's properties.
</para>
<para>
If the named class does not exist or does not implement the
<interfacename>MongoDB\BSON\Persistable</interfacename> interface,
<classname>stdClass</classname> will be used and each BSON document
key (including <property>__pclass</property>) will be set as a
public <classname>stdClass</classname> property.
</para>
<para>
The <property>__pclass</property> functionality relies on the
property being part of a retrieved MongoDB document. If you use a
<link linkend="mongodb-driver-query.construct-queryOptions">projection</link>
when querying for documents, you need to include the
<property>__pclass</property> field in the projection for this
functionality to work.
</para>
</listitem>
</itemizedlist>
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>"array"</literal></term>
<listitem>
<para>
Turns a BSON array or BSON document into a PHP array. There will be no
special treatment of a <property>__pclass</property> property <footnoteref linkend="mongodb.pclass"/>,
but it may be set as an element in the returned array if it was
present in the BSON document.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>"object"</literal> or <literal>"stdClass"</literal></term>
<listitem>
<para>
Turns a BSON array or BSON document into a
<classname>stdClass</classname> object. There will be no special
treatment of a <property>__pclass</property> property <footnoteref linkend="mongodb.pclass"/>, but it may
be set as a public property in the returned object if it was present
in the BSON document.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>"bson"</literal></term>
<listitem>
<para>
Turns a BSON array into a <classname>MongoDB\BSON\PackedArray</classname>
and a BSON document into a <classname>MongoDB\BSON\Document</classname>,
regardless of whether the BSON document has a <property>__pclass</property>
property <footnoteref linkend="mongodb.pclass"/>.
</para>
<note>
<simpara>
The <literal>bson</literal> value is only available for the three root
types, not in the field specific mappings.
</simpara>
</note>
</listitem>
</varlistentry>
<varlistentry>
<term>any other string</term>
<listitem>
<para>
Defines the class name that the BSON array or BSON object should be
deserialized as. For BSON objects that include
<property>__pclass</property> properties, that class will take
priority.
</para>
<para>
If the named class does not exist, is not concrete (i.e. it is
abstract or an interface), or does not implement
<interfacename>MongoDB\BSON\Unserializable</interfacename> then an
<classname>MongoDB\Driver\Exception\InvalidArgumentException</classname>
exception is thrown.
</para>
<para>
If the BSON object has a <property>__pclass</property> property and
that class exists and implements
<interfacename>MongoDB\BSON\Persistable</interfacename> it will
supersede the class provided in the type map.
</para>
<para>
The properties of the BSON document, <emphasis>including</emphasis>
the <property>__pclass</property> property if it exists, will be sent
as an associative array to the
<methodname>MongoDB\BSON\Unserializable::bsonUnserialize</methodname>
function to initialise the object's properties.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
<section xml:id="mongodb.persistence.typemaps">
<title>TypeMaps</title>
<para>
TypeMaps can be set through the
<methodname>MongoDB\Driver\Cursor::setTypeMap</methodname> method on a
<classname>MongoDB\Driver\Cursor</classname> object, or the
<literal>$typeMap</literal> argument of
<function>MongoDB\BSON\toPHP</function>,
<methodname>MongoDB\BSON\Document::toPHP</methodname>, and
<methodname>MongoDB\BSON\PackedArray::toPHP</methodname>. Each of the three
classes (<emphasis>root</emphasis>, <emphasis>document</emphasis>, and
<emphasis>array</emphasis>) can be individually set, in addition to the
field specific types.
</para>
<para>
If the value in the map is <type>NULL</type>, it means the same as the
<emphasis>default</emphasis> value for that item.
</para>
</section>
<section>
<title>Examples</title>
<para>
These examples use the following classes:
</para>
<para>
<variablelist>
<varlistentry>
<term>MyClass</term>
<listitem>
<para>
which does <emphasis>not</emphasis> implement any interface
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>YourClass</term>
<listitem>
<para>
which implements
<interfacename>MongoDB\BSON\Unserializable</interfacename>
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>OurClass</term>
<listitem>
<para>
which implements
<interfacename>MongoDB\BSON\Persistable</interfacename>
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>TheirClass</term>
<listitem>
<para>
which extends OurClass
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
<para>
The <methodname>MongoDB\BSON\Unserializable::bsonUnserialize</methodname>
method of YourClass, OurClass, TheirClass iterate over the array and set
the properties without modifications. It <emphasis>also</emphasis> sets
the <literal>$unserialized</literal> property to &true;:
<programlisting role="php">
<![CDATA[
<?php
function bsonUnserialize( array $map )
{
foreach ( $map as $k => $value )
{
$this->$k = $value;
}
$this->unserialized = true;
}
]]>
</programlisting>
</para>
<para>
<programlisting role="text">
<![CDATA[
/* typemap: [] (all defaults) */
{ "foo": "yes", "bar" : false }
-> stdClass { $foo => 'yes', $bar => false }
{ "foo": "no", "array" : [ 5, 6 ] }
-> stdClass { $foo => 'no', $array => [ 5, 6 ] }
{ "foo": "no", "obj" : { "embedded" : 3.14 } }
-> stdClass { $foo => 'no', $obj => stdClass { $embedded => 3.14 } }
{ "foo": "yes", "__pclass": "MyClass" }
-> stdClass { $foo => 'yes', $__pclass => 'MyClass' }
{ "foo": "yes", "__pclass": { "$type" : "80", "$binary" : "MyClass" } }
-> stdClass { $foo => 'yes', $__pclass => Binary(0x80, 'MyClass') }
{ "foo": "yes", "__pclass": { "$type" : "80", "$binary" : "YourClass") }
-> stdClass { $foo => 'yes', $__pclass => Binary(0x80, 'YourClass') }
{ "foo": "yes", "__pclass": { "$type" : "80", "$binary" : "OurClass") }
-> OurClass { $foo => 'yes', $__pclass => Binary(0x80, 'OurClass'), $unserialized => true }
{ "foo": "yes", "__pclass": { "$type" : "44", "$binary" : "YourClass") }
-> stdClass { $foo => 'yes', $__pclass => Binary(0x44, 'YourClass') }
]]>
</programlisting>
</para>
<para>
<programlisting role="text">
<![CDATA[
/* typemap: [ "root" => "MissingClass" ] */
{ "foo": "yes" }
-> MongoDB\Driver\Exception\InvalidArgumentException("MissingClass does not exist")
/* typemap: [ "root" => "MyClass" ] */
{ "foo": "yes", "__pclass" : { "$type": "80", "$binary": "MyClass" } }
-> MongoDB\Driver\Exception\InvalidArgumentException("MyClass does not implement Unserializable interface")
/* typemap: [ "root" => "MongoDB\BSON\Unserializable" ] */
{ "foo": "yes" }
-> MongoDB\Driver\Exception\InvalidArgumentException("Unserializable is not a concrete class")
/* typemap: [ "root" => "YourClass" ] */
{ "foo": "yes", "__pclass" : { "$type": "80", "$binary": "MongoDB\BSON\Unserializable" } }
-> YourClass { $foo => "yes", $__pclass => Binary(0x80, "MongoDB\BSON\Unserializable"), $unserialized => true }
/* typemap: [ "root" => "YourClass" ] */
{ "foo": "yes", "__pclass" : { "$type": "80", "$binary": "MyClass" } }
-> YourClass { $foo => "yes", $__pclass => Binary(0x80, "MyClass"), $unserialized => true }
/* typemap: [ "root" => "YourClass" ] */
{ "foo": "yes", "__pclass" : { "$type": "80", "$binary": "OurClass" } }
-> OurClass { $foo => "yes", $__pclass => Binary(0x80, "OurClass"), $unserialized => true }
/* typemap: [ "root" => "YourClass" ] */
{ "foo": "yes", "__pclass" : { "$type": "80", "$binary": "TheirClass" } }
-> TheirClass { $foo => "yes", $__pclass => Binary(0x80, "TheirClass"), $unserialized => true }
/* typemap: [ "root" => "OurClass" ] */
{ foo: "yes", "__pclass" : { "$type": "80", "$binary": "TheirClass" } }
-> TheirClass { $foo => "yes", $__pclass => Binary(0x80, "TheirClass"), $unserialized => true }
]]>
</programlisting>
</para>
<para>
<programlisting role="text">
<![CDATA[
/* typemap: [ 'root' => 'YourClass' ] */
{ foo: "yes", "__pclass" : { "$type": "80", "$binary": "YourClass" } }
-> YourClass { $foo => 'yes', $__pclass => Binary(0x80, 'YourClass'), $unserialized => true }
]]>
</programlisting>
</para>
<para>
<programlisting role="text">
<![CDATA[
/* typemap: [ 'root' => 'array', 'document' => 'array' ] */
{ "foo": "yes", "bar" : false }
-> [ "foo" => "yes", "bar" => false ]
{ "foo": "no", "array" : [ 5, 6 ] }
-> [ "foo" => "no", "array" => [ 5, 6 ] ]
{ "foo": "no", "obj" : { "embedded" : 3.14 } }
-> [ "foo" => "no", "obj" => [ "embedded => 3.14 ] ]
{ "foo": "yes", "__pclass": "MyClass" }
-> [ "foo" => "yes", "__pclass" => "MyClass" ]
{ "foo": "yes", "__pclass" : { "$type": "80", "$binary": "MyClass" } }
-> [ "foo" => "yes", "__pclass" => Binary(0x80, "MyClass") ]
{ "foo": "yes", "__pclass" : { "$type": "80", "$binary": "OurClass" } }
-> [ "foo" => "yes", "__pclass" => Binary(0x80, "OurClass") ]
]]>
</programlisting>
</para>
<para>
<programlisting role="text">
<![CDATA[
/* typemap: [ 'root' => 'object', 'document' => 'object' ] */
{ "foo": "yes", "__pclass": { "$type": "80", "$binary": "MyClass" } }
-> stdClass { $foo => "yes", "__pclass" => Binary(0x80, "MyClass") }
]]>
</programlisting>
</para>
</section>
</section>
</section>
</chapter>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
|