MongoDB\BSON\Serializable::bsonSerialize - Provides an array or document to serialize as BSON
Вернуться к: MongoDB\BSON\Serializable
MongoDB\BSON\Serializable::bsonSerialize
(mongodb >=1.0.0)
MongoDB\BSON\Serializable::bsonSerialize — Provides an array or document to serialize as BSON
Описание
Called during serialization of the object to BSON. The method must return an array or stdClass.
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 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.
Список параметров
У этой функции нет параметров.
Возвращаемые значения
An array or stdClass to be serialized as a BSON array or document.
Примеры
Пример #1 MongoDB\BSON\Serializable::bsonSerialize() returning an associative array for root document
<?php
class MyDocument implements MongoDB\BSON\Serializable
{
private $id;
function __construct()
{
$this->id = new MongoDB\BSON\ObjectID;
}
function bsonSerialize()
{
return ['_id' => $this->id, 'foo' => 'bar'];
}
}
$bson = MongoDB\BSON\fromPHP(new MyDocument);
echo MongoDB\BSON\toJSON($bson), "\n";
?>
Результатом выполнения данного примера будет что-то подобное:
{ "_id" : { "$oid" : "56cccdcada14d8755a58c591" }, "foo" : "bar" }
Пример #2 MongoDB\BSON\Serializable::bsonSerialize() returning a sequential array for root document
<?php
class MyArray implements MongoDB\BSON\Serializable
{
function bsonSerialize()
{
return [1, 2, 3];
}
}
$bson = MongoDB\BSON\fromPHP(new MyArray);
echo MongoDB\BSON\toJSON($bson), "\n";
?>
Результат выполнения данного примера:
{ "0" : 1, "1" : 2, "2" : 3 }
Пример #3 MongoDB\BSON\Serializable::bsonSerialize() returning an associative array for document field
<?php
class MyDocument implements MongoDB\BSON\Serializable
{
function bsonSerialize()
{
return ['foo' => 'bar'];
}
}
$value = ['document' => new MyDocument];
$bson = MongoDB\BSON\fromPHP($value);
echo MongoDB\BSON\toJSON($bson), "\n";
?>
Результат выполнения данного примера:
{ "document" : { "foo" : "bar" } }
Пример #4 MongoDB\BSON\Serializable::bsonSerialize() returning a sequential array for document field
<?php
class MyArray implements MongoDB\BSON\Serializable
{
function bsonSerialize()
{
return [1, 2, 3];
}
}
$value = ['array' => new MyArray];
$bson = MongoDB\BSON\fromPHP($value);
echo MongoDB\BSON\toJSON($bson), "\n";
?>
Результат выполнения данного примера:
{ "array" : [ 1, 2, 3 ] }
Смотрите также
- MongoDB\BSON\Unserializable::bsonUnserialize() - Constructs the object from a BSON array or document
- MongoDB\BSON\Persistable
- Persisting Data
Вернуться к: MongoDB\BSON\Serializable