forked from PHP-on-Couch/PHP-on-Couch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
02_documents_basics.php
217 lines (166 loc) · 5.89 KB
/
02_documents_basics.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/php -q
<?PHP
/**
This script demonstrates the basics of storing and retrieving documents using PHP On Couch
For this to work, you have to tell the database DSN (example : http://couch.server.com:5984/) and the name of a database that does not exist
*/
//Setup an autoloader (using src/Autoload.php)
$srcDir = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'src';
require $srcDir . DIRECTORY_SEPARATOR . 'Autoload.php';
### ANON DSN
$couchDsn = "http://localhost:5984/";
### AUTHENTICATED DSN
//$couchDsn = "http://admin:admin@localhost:5984/";
$couchDB = "example";
//Import required libraries
use PHPOnCouch\CouchClient;
use PHPOnCouch\Exceptions\CouchException;
/**
* create the client
*/
$client = new CouchClient($couchDsn,$couchDB);
/**
* As usual we create the database
*
*
*/
echo "#### Creating database ".$client->getDatabaseUri().': $result = $client->createDatabase();'."\n";
try {
$result = $client->createDatabase();
} catch (CouchException $e) {
echo "We issued the request, but couch server returned an error.\n";
echo "We can have HTTP Status code returned by couchDB using \$e->getCode() : ". $e->getCode()."\n";
echo "We can have error message returned by couchDB using \$e->getMessage() : ". $e->getMessage()."\n";
echo "Finally, we can have CouchDB's complete response body using \$e->getBody() : ". print_r($e->getBody(),true)."\n";
echo "Are you sure that your CouchDB server is at $couchDsn, and that database $couchDB does not exist ?\n";
exit (1);
}
catch(Exception $e){
echo "It seems that something wrong happened. You can have more details using :\n";
echo "the exception class with get_class(\$e) : ".get_class($e)."\n";
echo "the exception error code with \$e->getCode() : ".$e->getCode()."\n";
echo "the exception error message with \$e->getMessage() : ".$e->getMessage()."\n";
exit (1);
}
echo "Database successfully created. CouchDB sent the response :".print_r($result,true)."\n";
/**
* We now want to store a document.
* The first step is to have a PHP object with properties being the document property.
*
* To store the document, we call the method $client->storeDoc($doc)
*
* Important : if the object got an _id property, it'll be the unique document id in the couchdb database.
* If _id is not set, CouchDB will choose one for us.
*
*/
echo "#### Storing a document\n";
$doc = new stdClass();
$doc->_id = "some_doc";
$doc->title = "Important documentation";
$doc->tags = array("documentation","secret");
echo "Storing \$doc : \$client->storeDoc(\$doc)\n";
try {
$response = $client->storeDoc($doc);
} catch (Exception $e) {
echo "Something weird happened: ".$e->getMessage()." (errcode=".$e->getCode().")\n";
exit(1);
}
echo "The document is stored. CouchDB response body: ".print_r($response,true)."\n";
echo "#### Storing a document without _id property\n";
$doc = new stdClass();
$doc->title = "not really documentation";
$doc->tags = array("documentation","fake");
echo "Storing \$doc : \$client->storeDoc(\$doc)\n";
try {
$response = $client->storeDoc($doc);
} catch (Exception $e) {
echo "Something weird happened: ".$e->getMessage()." (errcode=".$e->getCode().")\n";
exit(1);
}
echo "The document is stored. CouchDB response body: ".print_r($response,true)."\n";
echo "CouchDB created the unique identifier ".$response->id." for this document\n";
/**
* Updating a document
*
* To update an existing document, you should tell CouchDB the latest revision of the document.
* That way, if someone updated the document between the time you read it (to get revision & infos)
* and the time you update it, your update will fail and you'll know the document changed.
*
*
*/
echo "==== using previous \$doc object to update it\n";
echo "Adding revison and id properties to the object :\n";
echo "\$doc->_id = \$response->id;\n";
$doc->_id = $response->id;
echo "\$doc->_rev = \$response->rev;\n";
$doc->_rev = $response->rev;
echo "Making a change : \$doc->tags[] = \"updated\";\n";
$doc->tags[] = "updated";
echo "==== storing document\n";
echo "Storing \$doc : \$client->storeDoc(\$doc)\n";
try {
$response = $client->storeDoc($doc);
} catch (Exception $e) {
echo "Something weird happened: ".$e->getMessage()." (errcode=".$e->getCode().")\n";
exit(1);
}
echo "The document is stored. CouchDB response body: ".print_r($response,true)."\n";
echo "The revision property changed : ".$response->rev."\n";
/**
* If we update once again $doc without updating _rev couchDB returns a 409 "Document update conflict"
*
*
*/
echo "#### Storing doc without updating _rev property (this should gice an error)\n";
try {
$result = $client->storeDoc($doc);
} catch (Exception $e ) {
echo "The document update failed: ".$e->getMessage()." (errcode=".$e->getCode().")\n";
}
/**
* To retrieve a document, just use getDoc($doc_id)
*
*
*
*
*/
echo "#### Getting document: some_doc\n";
echo "getting doc : \$doc = \$client->getDoc('some_doc')\n";
try {
$doc = $client->getDoc('some_doc');
} catch (Exception $e) {
if ( $e->code() == 404 ) {
echo "Document \"some_doc\" not found\n";
} else {
echo "Something weird happened: ".$e->getMessage()." (errcode=".$e->getCode().")\n";
}
exit(1);
}
echo "Document retrieved: ".print_r($doc,true)."\n";
/**
* Finally, deleting a document is achieved using deleteDoc
*
*
*
*/
echo "#### Deleting document \"some_doc\"\n";
echo "delete using previous \$doc object : \$client->deleteDoc(\$doc)\n";
try {
$result = $client->deleteDoc($doc);
} catch (Exception $e) {
echo "Something weird happened: ".$e->getMessage()." (errcode=".$e->getCode().")\n";
exit(1);
}
echo "Doc deleted, CouchDB response body: ".print_r($result,true)."\n";
/**
* Finally delete database
*
*
*
*/
try {
$result = $client->deleteDatabase();
} catch ( Exception $e) {
echo "Something weird happened: ".$e->getMessage()." (errcode=".$e->getCode().")\n";
exit(1);
}