Skip to content

Losing metadata when updating an object implementing Persistable #104

@gquemener

Description

@gquemener

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions