Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions firestore/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ To run the Cloud Firestore Samples:
subcollection-ref Get a reference to a subcollection document.
update-doc Update a document.
update-doc-array Update a document array field.
update-doc-increment Update a document number field using Increment.
update-nested-fields Update fields in nested data.
update-server-timestamp Update field with server timestamp.
where-order-by-limit-query Combine where with order by and limit in a query.
Expand Down
17 changes: 17 additions & 0 deletions firestore/firestore.php
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,23 @@
})
);

// Update Document Increment command
$application->add((new Command('update-doc-increment'))
->setDefinition($inputDefinition)
->setDescription('Update a document number field using Increment.')
->setHelp(<<<EOF
The <info>%command.name%</info> command updates a document number field using the Google Cloud Firestore API.

<info>php %command.full_name%</info>

EOF
)
->setCode(function ($input, $output) {
$projectId = $input->getArgument('project');
update_doc_increment($projectId);
})
);

// Set Document Merge command
$application->add((new Command('set-document-merge'))
->setDefinition($inputDefinition)
Expand Down
50 changes: 50 additions & 0 deletions firestore/src/update_doc_increment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* Copyright 2019 Google LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* For instructions on how to run the full sample:
*
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/firestore/README.md
*/

namespace Google\Cloud\Samples\Firestore;

use Google\Cloud\Firestore\FieldValue;
use Google\Cloud\Firestore\FirestoreClient;

/**
* Update a document array field.
* ```
* update_doc_array('your-project-id');
* ```
*/
function update_doc_increment($projectId)
{
// Create the Cloud Firestore client
$db = new FirestoreClient([
'projectId' => $projectId,
]);
# [START fs_update_doc_array]
$cityRef = $db->collection('cities')->document('DC');

// Atomically increment the population of the city by 50.
$cityRef->update([
['path' => 'regions', 'value' => FieldValue::incremet(50)]
]);
# [END fs_update_doc_array]
printf('Updated the population of the DC document in the cities collection.' . PHP_EOL);
}