-
Type: Bug
-
Resolution: Done
-
Priority: Major - P3
-
Affects Version/s: 1.1.2
-
Component/s: None
-
None
-
Environment:PHP 7.0.0, Yosemite
On update the driver writes the new embedded object as stdClass, ignoring BSON\Unserializable implementation.
<?php class Book implements MongoDB\BSON\Persistable { function bsonSerialize() { $data = get_object_vars($this); return $data; } function bsonUnserialize(array $data) { foreach ($data as $name => $value) { $this->{$name} = $value; } } } class Page implements MongoDB\BSON\Persistable { function bsonSerialize() { $data = get_object_vars($this); return $data; } function bsonUnserialize(array $data) { foreach ($data as $name => $value) { $this->{$name} = $value; } } } // Aux $manager = new MongoDB\Driver\Manager('mongodb://127.0.0.1:27017'); $bulk = new MongoDB\Driver\BulkWrite; $wc = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY); $collection = 'db.books'; // Delete previous entries $bulk->delete([], ['limit' => 0]); $manager->executeBulkWrite($collection, $bulk, $wc); // Create $book = new Book(); $book->title = 'Unnameable'; $book->pages = []; $page1 = new Page(); $page1->content = 'Lorem ipsum'; $book->pages[] = $page1; $result = $bulk->insert($book); $result = $manager->executeBulkWrite($collection, $bulk, $wc); // Read $query = new MongoDB\Driver\Query(['title' => $book->title]); $cursor = $manager->executeQuery($collection, $query); $bookAfterInsert = $cursor->toArray(); // hack to make update work properly $book = $bookAfterInsert[0]; // Update $book->description = 'An interesting document'; $page2 = new Page(); $page2->content = 'Dolor sit amet'; $book->pages[] = $page2; $bulk->update(['title' => $book->title], $book); $manager->executeBulkWrite($collection, $bulk, $wc); // Read (again) $query = new MongoDB\Driver\Query(['title' => $book->title]); $cursor = $manager->executeQuery($collection, $query); $bookAfterUpdate = $cursor->toArray(); var_dump($bookAfterUpdate[0]);
Expected result
object(Book)#%d (%d) { ["_id"]=> object(MongoDB\BSON\ObjectID)#%d (%d) { ["oid"]=> string(24) "%s" } ["__pclass"]=> object(MongoDB\BSON\Binary)#%d (%d) { ["data"]=> string(4) "Book" ["type"]=> int(%d) } ["title"]=> string(10) "Unnameable" ["pages"]=> array(2) { [0]=> object(Page)#%d (%d) { ["__pclass"]=> object(MongoDB\BSON\Binary)#%d (%d) { ["data"]=> string(4) "Page" ["type"]=> int(%d) } ["content"]=> string(11) "Lorem ipsum" } [1]=> object(Page)#%d (%d) { ["__pclass"]=> object(MongoDB\BSON\Binary)#%d (%d) { ["data"]=> string(4) "Page" ["type"]=> int(%d) } ["content"]=> string(14) "Dolor sit amet" } } ["description"]=> string(23) "An interesting document" }
Actual result
object(Book)#20 (5) { ["_id"]=> object(MongoDB\BSON\ObjectID)#15 (1) { ["oid"]=> string(24) "56a0591c5853e9198118c281" } ["__pclass"]=> object(MongoDB\BSON\Binary)#16 (2) { ["data"]=> string(4) "Book" ["type"]=> int(128) } ["title"]=> string(10) "Unnameable" ["pages"]=> array(2) { [0]=> object(Page)#18 (2) { ["__pclass"]=> object(MongoDB\BSON\Binary)#17 (2) { ["data"]=> string(4) "Page" ["type"]=> int(128) } ["content"]=> string(11) "Lorem ipsum" } [1]=> object(stdClass)#19 (1) { ["content"]=> string(14) "Dolor sit amet" } } ["description"]=> string(23) "An interesting document" }