mongo-php-transistor
, (*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)
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)