Skip to content

Commit 9a075dc

Browse files
committed
Example of a wishful API
1 parent 173a88a commit 9a075dc

File tree

2 files changed

+151
-0
lines changed

2 files changed

+151
-0
lines changed

docs/examples/changing-types.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
$typemap = array(
4+
\BSON\Types\ArrayObj::CLASS => \BSON\ArrayDynamic::CLASS,
5+
/* \BSON\Types\ArrayObj::CLASS => \BSON\ArrayFixed::CLASS, */
6+
/* \BSON\Types\ArrayObj::CLASS => "array", */
7+
\BSON\Types\Object::CLASS => \BSON\Object::CLASS,
8+
\BSON\Types\ObjectID::CLASS => \BSON\ObjectID::CLASS,
9+
\BSON\Types\DateTime::CLASS => \BSON\DateTime::CLASS,
10+
);
11+
12+
$mm = new MongoManager(array("bson" => $typemap));
13+
14+
15+

docs/examples/crud.php

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<?php
2+
class Person implements Mongolizable {
3+
protected $name;
4+
protected $nick;
5+
protected $addresses = array();
6+
7+
function __construct($name, $nick, array $addresses) {
8+
$this->name = $name;
9+
$this->nick = $nick;
10+
foreach($addresses as $address) {
11+
if ($address instanceof Address) {
12+
$this->addresses[] = $address;
13+
}
14+
}
15+
}
16+
17+
function getAddresses() {
18+
return $this->addresses;
19+
}
20+
}
21+
class Address implements Mongolizable {
22+
protected $address;
23+
protected $country;
24+
25+
function __construct($address, Country $country) {
26+
$this->address = $address;
27+
$this->country = $country;
28+
}
29+
function getCountry() {
30+
return $this->country;
31+
}
32+
}
33+
34+
class Country implements Mongolizable {
35+
protected $name;
36+
37+
function __construct($name) {
38+
$this->name;
39+
}
40+
41+
function getName() {
42+
return $name;
43+
}
44+
}
45+
46+
47+
$norway = new Country("Norway");
48+
$iceland = new Country("Iceland");
49+
$usa = new Country("USA");
50+
51+
$menlo = new Address("22 Coleman Pl", $usa);
52+
53+
$addresses = array(
54+
new Address("Dynekilgata 15", $norway),
55+
new Address("Mánabraut 4", $iceland),
56+
$menlo,
57+
);
58+
59+
$hannes = new Person("Hannes", "bjori", $addresses);
60+
$hayley = new Person("Hayley", "Ninja", array($menlo));
61+
62+
63+
64+
65+
/* Read/Write general options */
66+
$writeOptions = array(
67+
"ordered" => true,
68+
"writeConcern" => array(
69+
"w" => 3,
70+
"wtimeout" => 42,
71+
),
72+
);
73+
$readPreference = array(
74+
"type" => \MongoDB\ReadPreference\SECONDARY_PREFERRED,
75+
"tags" => array(
76+
array("dc:eu", "cpu:fast"),
77+
array("dc:eu", "cc:is"),
78+
array(""),
79+
),
80+
);
81+
82+
83+
84+
85+
/* Constructing a query */
86+
$query = array(
87+
'$query' => array("name" => "Hannes"),
88+
'$orderBy' => array('name' => 1),
89+
'$comment' => 'More special stuff',
90+
'$select' => array("fieldname" => 1, "fieldname.two" => 1), /* Internally rewritten to wire protocol option */
91+
'$skip' => 0, /* Don't skip any docs */ /* Internally rewritten to wire protocol option */
92+
'$limit' => -3, /* Kill server cursor after 3 docs */ /* Internally rewritten to wire protocol option */
93+
'$flags' => EXHAUST | NO_CURSOR_TIMEOUT, /* Internally rewritten to wire protocol option */
94+
);
95+
96+
$queryHannes = new Query($query);
97+
$queryUSA = new Query(array('$query' => array('addresses.$.Country' => "USA")));
98+
99+
100+
101+
102+
/* Main object. Manager instance / MongoClient */
103+
$mm = new MongoDB\Manager("mongodb://server1,server2/?replicaSet=FOOBAR");
104+
105+
106+
107+
$batch = new MongoDB\Batch\Write;
108+
$batch->add($hannes)->add($hayley);
109+
110+
$results = $mm->executeWrite("myproject.people", $batch, $writeOptions);
111+
$results instanceof WriteResults;
112+
113+
echo "Wrote ", $results->getOpCount(), " to ", $results->getServer()->getHostname(), " in ", $results->getTimer(), " ms\n";
114+
115+
116+
117+
$results = $mm->executeQuery("myproject.people", $queryHannes, $readPreference);
118+
$results instanceof QueryResults;
119+
120+
121+
122+
foreach($results as $person) {
123+
echo $person->getName() , " has lived in ", $person->getAddresses()[0]->getCountry()->getName(), "\n";
124+
}
125+
126+
127+
128+
129+
130+
131+
132+
133+
134+
135+
136+

0 commit comments

Comments
 (0)