@@ -154,7 +154,8 @@ public function add($collectionId, Document $document, $create = NULL) {
154
154
* @return bool - always true, will throw if there is an error
155
155
*/
156
156
public function update ($ collectionId , Document $ document , $ policy = NULL ) {
157
- $ documentId = $ this ->getDocumentId ($ document );
157
+ $ collectionId = $ this ->getCollectionId ($ document );
158
+ $ documentId = $ this ->getDocumentId ($ document );
158
159
159
160
if ($ policy === NULL ) {
160
161
$ policy = $ this ->getConnection ()->getOption (ConnectionOptions::OPTION_UPDATE_POLICY );
@@ -193,10 +194,10 @@ public function delete($collectionId, $document) {
193
194
* @param mixed $document - document id OR document to be updated
194
195
* @return mixed - document id, will throw if there is an error
195
196
*/
196
-
197
197
private function getDocumentId ($ document ) {
198
198
if ($ document instanceof Document) {
199
- $ documentId = $ document ->getId ();
199
+ $ id = $ document ->getId ();
200
+ list (, $ documentId ) = explode ('/ ' , $ document ->getId (), 2 );
200
201
}
201
202
else {
202
203
$ documentId = $ document ;
@@ -208,5 +209,24 @@ private function getDocumentId($document) {
208
209
209
210
return $ documentId ;
210
211
}
212
+
213
+ /**
214
+ * Helper function to get a collection id from a document
215
+ *
216
+ * @throws ClientException
217
+ * @param Document $document - document id
218
+ * @return mixed - collection id, will throw if there is an error
219
+ */
220
+ private function getCollectionId (Document $ document ) {
221
+ $ id = $ document ->getId ();
222
+
223
+ list ($ collectionId ,) = explode ('/ ' , $ id , 2 );
224
+
225
+ if (!$ collectionId || !(is_string ($ collectionId ) || is_double ($ collectionId ) || is_int ($ collectionId ))) {
226
+ throw new ClientException ('Cannot alter a document without a document id ' );
227
+ }
228
+
229
+ return $ collectionId ;
230
+ }
211
231
212
232
}
0 commit comments