<?php
class Address implements BSON\Persistable {
protected $streetAddress;
protected $city;
protected $postalCode;
function __construct($streetAddress, $city, $postalCode) {
$this->streetAddress = $streetAddress;
$this->city = $city;
$this->postalCode = $postalCode;
}
function bsonUnserialize(array $array) {
foreach($array as $k => $v) {
$this->{$k} = $v;
}
return $this;
}
function bsonSerialize() {
return get_object_vars($this);
}
function getStreetAddress() {
return $this->streetAddress;
}
}
class Person implements BSON\Persistable {
protected $_id;
protected $username;
protected $email;
protected $name;
protected $addresses = array();
protected $_lastModified;
protected $_created;
function __construct($username, $email, $name) {
$this->username = $username;
$this->email = $email;
$this->setName($name);
$this->_id = new BSON\ObjectID();
}
function addAddress(Address $address) {
$this->addresses[] = $address;
}
function bsonUnserialize(array $array) {
$this->__original = $array;
foreach($array as $k => $v) {
$this->{$k} = $v;
}
}
function bsonSerialize() {
$props = get_object_vars($this);
unset($props["__original"]);
unset($props["_lastModified"]);
if (empty($this->__original)) {
$props["_created"] = new BSON\UTCDatetime(microtime(true) * 1000);
return $props;
}
$updated = array(
'$currentDate' => array(
"_lastModified" => array('$type' => "date"),
),
'$set' => array(),
);
foreach($props as $k => $v) {
if (!isset($this->__original[$k])) {
$updated['$set'] = array($k => $v);
continue;
}
if ($this->__original[$k] != $v) {
$updated['$set'] = array($k => $v);
continue;
}
}
return $updated;
}
function getName() {
return $this->name;
}
function setName($name) {
return $this->name = $name;
}
function getCreatedDateTime() {
return $this->_created->toDateTime();
}
function getLastModifiedDateTime() {
return $this->_lastModified->toDateTime();
}
function toArray() {
$props = get_object_vars($this);
unset($props["__original"]);
return $props;
}
function getAddresses() {
return $this->addresses;
}
}
$m = new MongoDB\Driver\Manager("mongodb:);
try {
$dropcoll = new MongoDB\Driver\Command(array("drop" => "people"));
$m->executeCommand("examples", $dropcoll);
} catch(Exception $e) {}
$bulk = new MongoDB\Driver\BulkWrite;
$lair77 = new Person("lair77", "claire77@example.net", "Claire Corwin");
$hillardhaven = new Address("4527 Kohler Square Apt. 316", "Hillardhaven", "02622-5175");
$lair77->addAddress($hillardhaven);
$bulk->insert($lair77);
$tabitha = new Person("tabitha.mohr", "mohr@example.org", "Tabitha Lehner");
$konopelskichester = new Address("76650 Mina Pass", "Konopelskichester", "69679-5471");
$tabitha->addAddress($konopelskichester);
$bulk->insert($tabitha);
$hartmann = new Person("hartmann", "hartmann@example.org", "Hartmann Dedrick");
$leannefurt = new Address("151 Delbert Hills Suite 923", "Leannefurt", "22036");
$hartmann->addAddress($leannefurt);
$prudencemouth = new Address("7042 Freida Springs", "Prudencemouth", "94805");
$hartmann->addAddress($prudencemouth);
$bulk->insert($hartmann);
$ena = new Person("ena", "sanford@example.net", "Frida Sanford");
$lawsonport = new Address("3656 Jenifer Field", "New Lawsonport", "16300");
$ena->addAddress($lawsonport);
$bulk->insert($ena);
$lockman = new Person("lockman", "lockman.alice@example.net", "Alice Lockman");
$herminia = new Address("37413 Kailee Spurs", "East Herminia", "22107");
$lockman->addAddress($herminia);
$bulk->insert($lockman);
$m->executeBulkWrite("examples.people", $bulk);
/* Iterate over all the `examples.people`,
* converting them back into Person objects -- and the nested Address object!
*/
$query = new MongoDB\Driver\Query(array());
foreach($m->executeQuery("examples.people", $query) as $person) {
echo $person->getName(), " has the following address(es):\n";
foreach($person->getAddresses() as $address) {
echo "\t", $address->getStreetAddress(), "\n";
}
echo "\n";
}
echo "-----\n";
$hartmannFilter = array("username" => "hartmann");
$queryHartmann = new MongoDB\Driver\Query($hartmannFilter);
$hartmann = $m->executeQuery("examples.people", $queryHartmann)->toArray()[0];
$hartmann->setName("Dr. " . $hartmann->getName());
$retval = $m->executeUpdate("examples.people", $hartmannFilter, $hartmann);
var_dump($hartmann);
printf("Updated %d person (%s)\n", $retval->getModifiedCount(), $hartmann->getName());
$queryAll = new MongoDB\Driver\Query(array());
$all = $m->executeQuery("examples.people", $queryAll)->toArray();
foreach($all as $person) {
if ($person->getName() == "Dr. Hartmann Dedrick") {
var_dump($person->toArray());
var_dump($person->getCreatedDateTime()->format(DATE_RSS));
var_dump($person->getLastModifiedDateTime()->format(DATE_RSS));
}
}