Skip to content

Commit 1b9f3e1

Browse files
authored
List subcollections sample + tests (GoogleCloudPlatform#574)
1 parent 7c995cb commit 1b9f3e1

File tree

5 files changed

+104
-0
lines changed

5 files changed

+104
-0
lines changed

firestore/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ To run the Cloud Firestore Samples:
8282
add-doc-data-after-auto-id Auto-generate an ID for a document, then add document data.
8383
add-doc-data-types Set document data with different data types.
8484
add-doc-data-with-auto-id Add document data with an auto-generated ID.
85+
add-subcollection Add a subcollection by creating a new document.
8586
batch-write Batch write.
8687
chained-query Create a query with chained clauses.
8788
collection-ref Get a collection reference.
@@ -104,6 +105,7 @@ To run the Cloud Firestore Samples:
104105
invalid-range-order-by-query An invalid range with order by query.
105106
invalid-range-query An example of an invalid range query.
106107
list Lists commands
108+
list-subcollections List subcollections of a document.
107109
multiple-cursor-conditions Set multiple cursor conditions.
108110
order-by-name-desc-limit-query Create an order by name descending with limit query.
109111
order-by-name-limit-query Create an order by name with limit query.

firestore/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"src/set_requires_id.php",
2121
"src/add_doc_data_with_auto_id.php",
2222
"src/add_doc_data_after_auto_id.php",
23+
"src/list_subcollections.php",
2324
"src/query_create_examples.php",
2425
"src/create_query_state.php",
2526
"src/create_query_capital.php",

firestore/firestore.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,48 @@
443443
})
444444
);
445445

446+
// Add Subcollection command
447+
$application->add((new Command('add-subcollection'))
448+
->setDefinition($inputDefinition)
449+
->setDescription('Add a subcollection by creating a new document.')
450+
->setHelp(<<
451+
The %command.name% command adds a subcollection by creating a new document using the Google Cloud Firestore API.
452+
453+
php %command.full_name%
454+
455+
EOF
456+
)
457+
->setCode(function ($input, $output) {
458+
$projectId = $input->getArgument('project');
459+
$db = new FirestoreClient([
460+
'projectId' => $projectId,
461+
]);
462+
$cityRef = $db->collection('cities')->document('SF');
463+
$subcollectionRef = $cityRef->collection('neighborhoods');
464+
$data = [
465+
'name' => 'Marina',
466+
];
467+
$subcollectionRef->document('Marina')->set($data);
468+
})
469+
);
470+
471+
// List Subcollections command
472+
$application->add((new Command('list-subcollections'))
473+
->setDefinition($inputDefinition)
474+
->setDescription('List subcollections of a document.')
475+
->setHelp(<<
476+
The %command.name% command lists subcollections of a document using the Google Cloud Firestore API.
477+
478+
php %command.full_name%
479+
480+
EOF
481+
)
482+
->setCode(function ($input, $output) {
483+
$projectId = $input->getArgument('project');
484+
list_subcollections($projectId);
485+
})
486+
);
487+
446488
// Order By Name Limit Query command
447489
$application->add((new Command('order-by-name-limit-query'))
448490
->setDefinition($inputDefinition)
@@ -816,6 +858,8 @@
816858
$db = new FirestoreClient([
817859
'projectId' => $projectId,
818860
]);
861+
$subcollection = $db->collection('cities/SF/neighborhoods');
862+
delete_collection($subcollection, 2);
819863
$cityCollection = $db->collection('cities');
820864
delete_collection($cityCollection, 2);
821865
$dataCollection = $db->collection('data');

firestore/src/list_subcollections.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
/**
3+
* Copyright 2018 Google Inc.
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\FirestoreClient;
27+
28+
/**
29+
* List subcollections of a document.
30+
* ```
31+
* list_subcollections('your-project-id');
32+
* ```
33+
*/
34+
function list_subcollections($projectId)
35+
{
36+
// Create the Cloud Firestore client
37+
$db = new FirestoreClient([
38+
'projectId' => $projectId,
39+
]);
40+
# [START fs_get_collections]
41+
$cityRef = $db->collection('cities')->document('SF');
42+
$collections = $cityRef->collections();
43+
foreach ($collections as $collection) {
44+
printf('Found subcollection with id: %s' . PHP_EOL, $collection->id());
45+
}
46+
# [END fs_get_collections]
47+
}

firestore/test/firestoreTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,16 @@ public function testGetAllDocs()
273273
$this->assertContains('Document data for document LA:', $output);
274274
$this->assertContains('[name] => Los Angeles', $output);
275275
}
276+
277+
/**
278+
* @depends testRetrieveCreateExamples
279+
*/
280+
public function testListSubcollections()
281+
{
282+
$this->runCommand('add-subcollection');
283+
$output = $this->runCommand('list-subcollections');
284+
$this->assertContains('Found subcollection with id: neighborhoods', $output);
285+
}
276286

277287
/**
278288
* @depends testRetrieveCreateExamples

0 commit comments

Comments
 (0)