Skip to content

Commit ba74b89

Browse files
committed
Merge pull request #1 from bjori/split-crud
Split and modify CRUD examples
2 parents 153be68 + eee840c commit ba74b89

File tree

4 files changed

+127
-136
lines changed

4 files changed

+127
-136
lines changed

docs/examples/crud.php

Lines changed: 0 additions & 136 deletions
This file was deleted.

docs/examples/insert.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
$ordered = true;
4+
5+
$insertBatch = new \MongoDB\Batch\InsertBatch($ordered);
6+
$insertBatch
7+
->add($hannes)
8+
->add($hayley);
9+
10+
$w = 'majority';
11+
$w = 500;
12+
13+
$writeConcern = new \MongoDB\WriteConcern($w, $wtimeout);
14+
15+
$mm = new \MongoDB\Manager("mongodb://server1,server2/?replicaSet=FOOBAR");
16+
17+
$result = $mm->executeWrite("db.collection", $insertBatch, $writeConcern);
18+
19+
assert($result instanceof \MongoDB\WriteResult);
20+
21+
printf(
22+
"Inserted %d documents %s in %d msec\n",
23+
$result->getOpCount(),
24+
$result->getServer()->getHostname(),
25+
$result->getTimer()
26+
);

docs/examples/model.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
class Person implements BsonSerializable {
4+
protected $name;
5+
protected $nick;
6+
protected $addresses = array();
7+
8+
function __construct($name, $nick, array $addresses) {
9+
$this->name = $name;
10+
$this->nick = $nick;
11+
foreach($addresses as $address) {
12+
if ($address instanceof Address) {
13+
$this->addresses[] = $address;
14+
}
15+
}
16+
}
17+
18+
function getAddresses() {
19+
return $this->addresses;
20+
}
21+
}
22+
23+
class Address implements BsonSerializable {
24+
protected $address;
25+
protected $country;
26+
27+
function __construct($address, Country $country) {
28+
$this->address = $address;
29+
$this->country = $country;
30+
}
31+
32+
function getCountry() {
33+
return $this->country;
34+
}
35+
}
36+
37+
class Country implements BsonSerializable {
38+
protected $name;
39+
40+
function __construct($name) {
41+
$this->name;
42+
}
43+
44+
function getName() {
45+
return $name;
46+
}
47+
}
48+
49+
$norway = new Country("Norway");
50+
$iceland = new Country("Iceland");
51+
$usa = new Country("USA");
52+
53+
$menlo = new Address("22 Coleman Pl", $usa);
54+
55+
$addresses = array(
56+
new Address("Dynekilgata 15", $norway),
57+
new Address("Mánabraut 4", $iceland),
58+
$menlo,
59+
);
60+
61+
$hannes = new Person("Hannes", "bjori", $addresses);
62+
$hayley = new Person("Hayley", "Ninja", array($menlo));

docs/examples/query.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
// Corresponds to "query object" in http://docs.mongodb.org/meta-driver/latest/legacy/mongodb-wire-protocol/#op-query
4+
$queryObj = array(
5+
'$query' => array("name" => "Hannes"),
6+
'$orderBy' => array('name' => 1),
7+
'$comment' => 'More special stuff',
8+
);
9+
10+
$query = new \MongoDB\Query($queryObj);
11+
$query
12+
->setSelector(array('fieldname' => 1, 'fieldname.two' => 1))
13+
->setLimit(-3) // Kill server cursor after 3 documents
14+
->setSkip(0) // Don't skip any documents
15+
->setFlags(EXHAUST | NO_CURSOR_TIMEOUT);
16+
17+
$tags = array(
18+
array("dc:eu", "cpu:fast"),
19+
array("dc:eu", "cc:is"),
20+
array(""),
21+
);
22+
23+
$readPreference = new \MongoDB\ReadPreference('secondaryPreferred', $tags);
24+
25+
$mm = new \MongoDB\Manager("mongodb://server1,server2/?replicaSet=FOOBAR");
26+
27+
$result = $mm->executeQuery("db.collection", $query, $readPreference);
28+
29+
assert($result instanceof \MongoDB\QueryResult);
30+
assert($result instanceof \IteratorAggregate);
31+
assert($result->getIterator() instanceof \MongoDB\Cursor);
32+
33+
foreach($result as $person) {
34+
printf(
35+
"%s has lived in %s\n",
36+
$person->getName(),
37+
$person->getAddresses()[0]->getCountry()->getName()
38+
);
39+
}

0 commit comments

Comments
 (0)