Skip to content

Commit 594dbdb

Browse files
committed
Split and modify CRUD examples
1 parent 153be68 commit 594dbdb

File tree

4 files changed

+130
-136
lines changed

4 files changed

+130
-136
lines changed

docs/examples/crud.php

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

docs/examples/insert.php

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

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: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
$queryDocument = array(
4+
'$query' => array("name" => "Hannes"),
5+
'$orderBy' => array('name' => 1),
6+
'$comment' => 'More special stuff',
7+
);
8+
9+
$queryFields = array(
10+
'fieldname' => 1,
11+
'fieldname.two' => 1,
12+
);
13+
14+
$flags = EXHAUST | NO_CURSOR_TIMEOUT;
15+
$limit = -3; // Kill server cursor after 3 documents
16+
$skip = 0; // Don't skip any documents
17+
18+
$query = new \MongoDB\Query($queryDocument, $queryFields, $flags, $limit, $skip);
19+
20+
$readPreference = array(
21+
"type" => 'secondaryPreferred',
22+
"tags" => array(
23+
array("dc:eu", "cpu:fast"),
24+
array("dc:eu", "cc:is"),
25+
array(""),
26+
),
27+
);
28+
29+
$mm = new \MongoDB\Manager("mongodb://server1,server2/?replicaSet=FOOBAR");
30+
31+
$cursor = $mm->executeQuery("db.collection", $query, $readPreference);
32+
33+
assert($cursor instanceof \MongoDB\Cursor);
34+
35+
foreach($cursor as $person) {
36+
printf(
37+
"%s has lived in %s\n",
38+
$person->getName(),
39+
$person->getAddresses()[0]->getCountry()->getName()
40+
);
41+
}

0 commit comments

Comments
 (0)