-
Notifications
You must be signed in to change notification settings - Fork 268
Closed
Description
Hello,
I'm having trouble updating one document that implements the Persistable interface.
It's unserialization metadata get lost every time I replace the document.
Here's an exemple showing the problem:
<?php
require 'vendor/autoload.php';
class A implements \MongoDB\BSON\Persistable
{
private $name;
public function __construct($name)
{
$this->name = $name;
}
public function setName($name)
{
$this->name = $name;
}
public function bsonSerialize()
{
return [
'name' => $this->name
];
}
public function bsonUnserialize(array $data)
{
$this->name = $data['name'];
}
}
$instance = new A('foo');
$manager = new \MongoDB\Driver\Manager('mongodb://mongo:27017');
$collection = new \MongoDB\Collection($manager, 'test', 'coll');
$result = $collection->insertOne($instance);
/**
* > db.coll.find()
* {
* "_id" : ObjectId("56a269e0046d5f0014645471"),
* "__pclass" : BinData(128,"QQ=="),
* "name" : "foo"
* }
*/
$instance->setName('bar');
$collection->replaceOne(['_id' => $result->getInsertedId()], $instance);
/**
* > db.coll.find()
* { "_id" : ObjectId("56a26a97046d5f0039107a21"), "name" : "bar" }
*/
What am I doing wrong here please?
I'm running mongodb server v3.2.1.
Thanks in advance!
Metadata
Metadata
Assignees
Labels
No labels