2017 © Pedro Peláez
 

library transistor

Simple MongoDB Object<->Document Mapper

image

mongodb/transistor

Simple MongoDB Object<->Document Mapper

  • Tuesday, February 16, 2016
  • by bjori
  • Repository
  • 4 Watchers
  • 20 Stars
  • 113 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 3 Forks
  • 3 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

mongo-php-transistor

Build Status, (*1)

The new PHP Driver for MongoDB provides a MongoDB\BSON\Persistable interface which declares two methods to be called when storing the object, and the other when re-constructing it., (*2)

This transistor trait adds example implementation of the two methods and introduces lightweight change tracking. This allows the object to be seamlessly updated as well., (*3)

Example classes


See Person.php and Address.php for the full implementation of these example classes -- although this is really it. No annotations or anything. implements MongoDB\BSON\Persistable and use MongoDB\Transistor is the magic., (*4)

Simple usage

 "bjori"));

/* Get an instance of the Person object again */
var_dump($person);
?>

The above example will output something similar to, (*5)

object(Person)#8 (7) {
  ["_id"]=>
  object(MongoDB\BSON\ObjectID)#4 (1) {
    ["oid"]=>
    string(24) "553586e2bd21b971774b7da1"
  }
  ["username"]=>
  string(5) "bjori"
  ["email"]=>
  string(13) "bjori@php.net"
  ["name"]=>
  string(16) "Hannes Magnusson"
  ["addresses"]=>
  array(0) {
  }
  ["_lastModified"]=>
  NULL
  ["_created"]=>
  object(MongoDB\BSON\UTCDatetime)#7 (0) {
  }
}

Updating the object

setName("Dr. " . $person->getName());

/* Update the document */
update(array("username" => "bjori"), $person);

/* Retrieve it again */
$person = findOne(array("username" => "bjori"));

/* Get an instance of the Person object again */
var_dump($person);
?>

The above example will output something similar to, (*6)

object(Person)#9 (7) {
  ["_id"]=>
  object(MongoDB\BSON\ObjectID)#4 (1) {
    ["oid"]=>
    string(24) "553586e2bd21b971774b7da1"
  }
  ["username"]=>
  string(5) "bjori"
  ["email"]=>
  string(13) "bjori@php.net"
  ["name"]=>
  string(16) "Dr. Hannes Magnusson"
  ["addresses"]=>
  array(0) {
  }
  ["_lastModified"]=>
  NULL
  ["_created"]=>
  object(MongoDB\BSON\UTCDatetime)#7 (0) {
  }
}

Adding embedded objects

addAddress($address);

/* Update the object with a new Address embedded object */
update(array("username" => "bjori"), $person);

$person = findOne(array("username" => "bjori"));
var_dump($person);
?>

The above example will output something similar to, (*7)

object(Person)#10 (7) {
  ["_id"]=>
  object(MongoDB\BSON\ObjectID)#4 (1) {
    ["oid"]=>
    string(24) "553586e2bd21b971774b7da1"
  }
  ["username"]=>
  string(5) "bjori"
  ["email"]=>
  string(13) "bjori@php.net"
  ["name"]=>
  string(16) "Dr. Hannes Magnusson"
  ["addresses"]=>
  array(1) {
    [0]=>
    object(Address)#%d (%d) {
      ["_id"]=>
      object(MongoDB\BSON\ObjectID)#%d (%d) {
        ["oid"]=>
        string(24) "%s"
      }
      ["streetAddress"]=>
      string(11) "Manabraut 4"
      ["city"]=>
      string(9) "Kopavogur"
      ["postalCode"]=>
      int(200)
      ["_created"]=>
      object(MongoDB\BSON\UTCDatetime)#%d (0) {
      }
    }
  }
  ["_lastModified"]=>
  NULL
  ["_created"]=>
  object(MongoDB\BSON\UTCDatetime)#7 (0) {
  }
}

Helpers

The insert(), update() and findOne() helpers in the example above don't do anything other then wrap their respective methods on the MongoDB\Driver\Manager and setting the TypeMap, and are only there to reduce error checking needed in the examples., (*8)

Performance

Since the actual object (un-)serialization is done by the extension itself there is nothing for PHP to do -- the trait itself is under 200 lines of dead simple code., (*9)

I'm sure there are dragons, so use with care., (*10)

The Versions

16/02 2016

dev-master

9999999-dev https://github.com/bjori/mongo-php-transistor

Simple MongoDB Object<->Document Mapper

  Sources   Download

BSD 2-Clause

The Requires

  • ext-mongodb ^1.1.0

 

orm mongodb database driver odm persistence

23/04 2015

0.1.0

0.1.0.0 https://github.com/bjori/mongo-php-transistor

Simple MongoDB Object<->Document Mapper

  Sources   Download

BSD 2-Clause

The Requires

  • ext-mongodb >=0.5

 

orm mongodb database driver odm persistence