Skip to content

Split and modify CRUD examples #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 13, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 0 additions & 136 deletions docs/examples/crud.php

This file was deleted.

26 changes: 26 additions & 0 deletions docs/examples/insert.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

$ordered = true;

$insertBatch = new \MongoDB\Batch\InsertBatch($ordered);
$insertBatch
->add($hannes)
->add($hayley);

$w = 'majority';
$w = 500;

$writeConcern = new \MongoDB\WriteConcern($w, $wtimeout);

$mm = new \MongoDB\Manager("mongodb://server1,server2/?replicaSet=FOOBAR");

$result = $mm->executeWrite("db.collection", $insertBatch, $writeConcern);

assert($result instanceof \MongoDB\WriteResult);

printf(
"Inserted %d documents %s in %d msec\n",
$result->getOpCount(),
$result->getServer()->getHostname(),
$result->getTimer()
);
62 changes: 62 additions & 0 deletions docs/examples/model.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

class Person implements BsonSerializable {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed Mongolizable to BsonSerializable. This mimics JsonSerializable and I don't see the harm in referring to BSON if we're already going to do it in the type mappings elsewhere.

protected $name;
protected $nick;
protected $addresses = array();

function __construct($name, $nick, array $addresses) {
$this->name = $name;
$this->nick = $nick;
foreach($addresses as $address) {
if ($address instanceof Address) {
$this->addresses[] = $address;
}
}
}

function getAddresses() {
return $this->addresses;
}
}

class Address implements BsonSerializable {
protected $address;
protected $country;

function __construct($address, Country $country) {
$this->address = $address;
$this->country = $country;
}

function getCountry() {
return $this->country;
}
}

class Country implements BsonSerializable {
protected $name;

function __construct($name) {
$this->name;
}

function getName() {
return $name;
}
}

$norway = new Country("Norway");
$iceland = new Country("Iceland");
$usa = new Country("USA");

$menlo = new Address("22 Coleman Pl", $usa);

$addresses = array(
new Address("Dynekilgata 15", $norway),
new Address("Mánabraut 4", $iceland),
$menlo,
);

$hannes = new Person("Hannes", "bjori", $addresses);
$hayley = new Person("Hayley", "Ninja", array($menlo));
39 changes: 39 additions & 0 deletions docs/examples/query.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

// Corresponds to "query object" in http://docs.mongodb.org/meta-driver/latest/legacy/mongodb-wire-protocol/#op-query
$queryObj = array(
'$query' => array("name" => "Hannes"),
'$orderBy' => array('name' => 1),
'$comment' => 'More special stuff',
);

$query = new \MongoDB\Query($queryObj);
$query
->setSelector(array('fieldname' => 1, 'fieldname.two' => 1))
->setLimit(-3) // Kill server cursor after 3 documents
->setSkip(0) // Don't skip any documents
->setFlags(EXHAUST | NO_CURSOR_TIMEOUT);

$tags = array(
array("dc:eu", "cpu:fast"),
array("dc:eu", "cc:is"),
array(""),
);

$readPreference = new \MongoDB\ReadPreference('secondaryPreferred', $tags);

$mm = new \MongoDB\Manager("mongodb://server1,server2/?replicaSet=FOOBAR");

$result = $mm->executeQuery("db.collection", $query, $readPreference);

assert($result instanceof \MongoDB\QueryResult);
assert($result instanceof \IteratorAggregate);
assert($result->getIterator() instanceof \MongoDB\Cursor);

foreach($result as $person) {
printf(
"%s has lived in %s\n",
$person->getName(),
$person->getAddresses()[0]->getCountry()->getName()
);
}