Skip to content

Commit 0da8594

Browse files
authored
Merge pull request GoogleCloudPlatform#945 from BenWhitehead/numeric-increment
docs(samples): Add `fs_update_document_increment` snippet
2 parents 7061b9f + f7470f3 commit 0da8594

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

firestore/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ To run the Cloud Firestore Samples:
128128
subcollection-ref Get a reference to a subcollection document.
129129
update-doc Update a document.
130130
update-doc-array Update a document array field.
131+
update-doc-increment Update a document number field using Increment.
131132
update-nested-fields Update fields in nested data.
132133
update-server-timestamp Update field with server timestamp.
133134
where-order-by-limit-query Combine where with order by and limit in a query.

firestore/firestore.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,23 @@
706706
})
707707
);
708708

709+
// Update Document Increment command
710+
$application->add((new Command('update-doc-increment'))
711+
->setDefinition($inputDefinition)
712+
->setDescription('Update a document number field using Increment.')
713+
->setHelp(<<
714+
The %command.name% command updates a document number field using the Google Cloud Firestore API.
715+
716+
php %command.full_name%
717+
718+
EOF
719+
)
720+
->setCode(function ($input, $output) {
721+
$projectId = $input->getArgument('project');
722+
update_doc_increment($projectId);
723+
})
724+
);
725+
709726
// Set Document Merge command
710727
$application->add((new Command('set-document-merge'))
711728
->setDefinition($inputDefinition)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
/**
3+
* Copyright 2019 Google LLC.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/firestore/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Firestore;
25+
26+
use Google\Cloud\Firestore\FieldValue;
27+
use Google\Cloud\Firestore\FirestoreClient;
28+
29+
/**
30+
* Update a document array field.
31+
* ```
32+
* update_doc_array('your-project-id');
33+
* ```
34+
*/
35+
function update_doc_increment($projectId)
36+
{
37+
// Create the Cloud Firestore client
38+
$db = new FirestoreClient([
39+
'projectId' => $projectId,
40+
]);
41+
# [START fs_update_doc_array]
42+
$cityRef = $db->collection('cities')->document('DC');
43+
44+
// Atomically increment the population of the city by 50.
45+
$cityRef->update([
46+
['path' => 'regions', 'value' => FieldValue::incremet(50)]
47+
]);
48+
# [END fs_update_doc_array]
49+
printf('Updated the population of the DC document in the cities collection.' . PHP_EOL);
50+
}

0 commit comments

Comments
 (0)