MongoDB\BSON\Serializable::bsonSerializeProvides an array or document to serialize as BSON 説明
abstract public arraystdClassMongoDB\BSON\DocumentMongoDB\BSON\PackedArray MongoDB\BSON\Serializable::bsonSerialize()
Called during serialization of the object to BSON. The method must return an array, stdClass, MongoDB\BSON\Document, or MongoDB\BSON\PackedArray.
Root documents (e.g. a
MongoDB\BSON\Serializable passed to
MongoDB\BSON\fromPHP) will always be serialized as a
BSON document. For field values, associative arrays and
stdClass instances will be serialized as a BSON
document and sequential arrays (i.e. sequential, numeric indexes starting at
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. パラメータこの関数にはパラメータはありません。 戻り値An array, stdClass, MongoDB\BSON\Document, or MongoDB\BSON\PackedArray to be serialized as a BSON array or document. 変更履歴
例例1 MongoDB\BSON\Serializable::bsonSerialize returning an associative array for root document
<?php 上の例の出力は、 たとえば以下のようになります。 { "_id" : { "$oid" : "56cccdcada14d8755a58c591" }, "foo" : "bar" } 例2 MongoDB\BSON\Serializable::bsonSerialize returning a sequential array for root document
<?php 上の例の出力は以下となります。 { "0" : 1, "1" : 2, "2" : 3 } 例3 MongoDB\BSON\Serializable::bsonSerialize returning an associative array for document field
<?php 上の例の出力は以下となります。 { "document" : { "foo" : "bar" } } 例4 MongoDB\BSON\Serializable::bsonSerialize returning a sequential array for document field
<?php 上の例の出力は以下となります。 { "array" : [ 1, 2, 3 ] } 参考
|