Uploaded image for project: 'PHP Driver: Extension'
  1. PHP Driver: Extension
  2. PHPC-546

should I discard an object after insert it?

    • Type: Icon: Task Task
    • Resolution: Duplicate
    • Priority: Icon: Trivial - P5 Trivial - P5
    • None
    • Affects Version/s: 1.1.2
    • Component/s: None
    • None
    • Environment:
      PHP 7.0.0, Yosemite

      After to insert a MongoDB\BSON\Persistable object, if I update the same instance, then this document is saved as stdClass. Mongodb lost the reference to my class.

      Things I've noticed:

      • If I manually inject the "__pclass" property, update works properly;
      • The behavior changes with "_id", when using the same object in a subsequent update, the "_id" is kept with its original value.
      <?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;
              }
          }
      }
      
      // 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';
      $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->__pclass = $bookAfterInsert[0]->__pclass;
      
      // Update
      $book->description = 'An interesting document';
      $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"
        ["description"]=>
        string(23) "An interesting document"
      }
      

      Actual result

      object(stdClass)#13 (3) {
        ["_id"]=>
        object(MongoDB\BSON\ObjectID)#12 (1) {
          ["oid"]=>
          string(24) "56a05eaa5853e919c3376c31"
        }
        ["title"]=>
        string(10) "Unnameable"
        ["description"]=>
        string(23) "An interesting document"
      }
      

            Assignee:
            jmikola@mongodb.com Jeremy Mikola
            Reporter:
            renanbr Renan de Lima Barbosa
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: