Skip to content

Commit cd9cc4c

Browse files
authored
feat(firestore): add missing samples (GoogleCloudPlatform#1097)
1 parent fee0c29 commit cd9cc4c

File tree

8 files changed

+441
-10
lines changed

8 files changed

+441
-10
lines changed

firestore/composer.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"require": {
3-
"google/cloud-firestore": "^1.12.1",
3+
"google/cloud-firestore": "^1.13",
44
"symfony/console": "^3.0"
55
},
66
"autoload": {
@@ -10,8 +10,11 @@
1010
"src/add_doc_data_types.php",
1111
"src/add_doc_data_with_auto_id.php",
1212
"src/array_membership.php",
13+
"src/array_membership_any.php",
1314
"src/batch_write.php",
1415
"src/chained_query.php",
16+
"src/collection_group_query.php",
17+
"src/collection_group_query_setup.php",
1518
"src/collection_ref.php",
1619
"src/composite_index_chained_query.php",
1720
"src/create_query_capital.php",
@@ -27,6 +30,8 @@
2730
"src/get_distributed_counter_value.php",
2831
"src/get_document.php",
2932
"src/get_multiple_docs.php",
33+
"src/in_array_query.php",
34+
"src/in_query.php",
3035
"src/initialize.php",
3136
"src/initialize_distributed_counter.php",
3237
"src/initialize_project_id.php",

firestore/firestore.php

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,91 @@
990990
})
991991
);
992992

993+
// Array Membership Any command
994+
$application->add((new Command('array-membership-any'))
995+
->setDefinition($inputDefinition)
996+
->setDescription('Create queries using an array-contains-any where clause.')
997+
->setHelp(<<
998+
The %command.name% command creates queries using an array-contains-any where clause using the Google Cloud Firestore API.
999+
1000+
php %command.full_name%
1001+
1002+
EOF
1003+
)
1004+
->setCode(function ($input, $output) {
1005+
$projectId = $input->getArgument('project');
1006+
array_membership_any($projectId);
1007+
})
1008+
);
1009+
1010+
// Where In Query command
1011+
$application->add((new Command('in-query'))
1012+
->setDefinition($inputDefinition)
1013+
->setDescription('Create queries using an IN where clause.')
1014+
->setHelp(<<
1015+
The %command.name% command creates queries using an IN where clause using the Google Cloud Firestore API.
1016+
1017+
php %command.full_name%
1018+
1019+
EOF
1020+
)
1021+
->setCode(function ($input, $output) {
1022+
$projectId = $input->getArgument('project');
1023+
in_query($projectId);
1024+
})
1025+
);
1026+
1027+
// Where In Array Query command
1028+
$application->add((new Command('in-array-query'))
1029+
->setDefinition($inputDefinition)
1030+
->setDescription('Create queries using an IN where clause containing array items.')
1031+
->setHelp(<<
1032+
The %command.name% command creates queries using an IN where clause with array items using the Google Cloud Firestore API.
1033+
1034+
php %command.full_name%
1035+
1036+
EOF
1037+
)
1038+
->setCode(function ($input, $output) {
1039+
$projectId = $input->getArgument('project');
1040+
in_array_query($projectId);
1041+
})
1042+
);
1043+
1044+
// Collection group query setup
1045+
$application->add((new Command('collection-group-query-setup'))
1046+
->setDefinition($inputDefinition)
1047+
->setDescription('Create example collection group for documents.')
1048+
->setHelp(<<
1049+
The %command.name% command creates example collection group for documents using the Google Cloud Firestore API.
1050+
1051+
php %command.full_name%
1052+
1053+
EOF
1054+
)
1055+
->setCode(function ($input, $output) {
1056+
$projectId = $input->getArgument('project');
1057+
collection_group_query_setup($projectId);
1058+
})
1059+
);
1060+
1061+
// Collection group query
1062+
$application->add((new Command('collection-group-query'))
1063+
->setDefinition($inputDefinition)
1064+
->setDescription('List documents with matching collection group')
1065+
->setHelp(<<
1066+
The %command.name% command lists documents with collection group having matching elements using the Google Cloud Firestore API.
1067+
1068+
php %command.full_name%
1069+
1070+
EOF
1071+
)
1072+
->setCode(function ($input, $output) {
1073+
$projectId = $input->getArgument('project');
1074+
collection_group_query($projectId);
1075+
})
1076+
);
1077+
9931078
// for testing
9941079
if (getenv('PHPUNIT_TESTS') === '1') {
9951080
return $application;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
/**
3+
* Copyright 2020 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\FirestoreClient;
27+
28+
/**
29+
* Create queries using an array-contains-any where clause.
30+
* ```
31+
* array_membership_any('your-project-id');
32+
* ```
33+
*/
34+
function array_membership_any(string $projectId): void
35+
{
36+
// Create the Cloud Firestore client
37+
$db = new FirestoreClient([
38+
'projectId' => $projectId,
39+
]);
40+
$citiesRef = $db->collection('cities');
41+
# [START fs_query_filter_array_contains_any]
42+
$containsQuery = $citiesRef->where('regions', 'array-contains-any', ['west_coast', 'east_coast']);
43+
# [END fs_query_filter_array_contains_any]
44+
foreach ($containsQuery->documents() as $document) {
45+
printf('Document %s returned by query regions array-contains-any [west_coast, east_coast]' . PHP_EOL, $document->id());
46+
}
47+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
/**
3+
* Copyright 2020 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\FirestoreClient;
27+
28+
/**
29+
* Query collection group for documents.
30+
* ```
31+
* collection_group_query('your-project-id');
32+
* ```
33+
*/
34+
function collection_group_query(string $projectId): void
35+
{
36+
// Create the Cloud Firestore client
37+
$db = new FirestoreClient([
38+
'projectId' => $projectId,
39+
]);
40+
41+
# [START fs_collection_group_query]
42+
$museums = $db->collectionGroup('landmarks')->where('type', '==', 'museum');
43+
foreach ($museums->documents() as $document) {
44+
printf('%s => %s' . PHP_EOL, $document->id(), $document->data()['name']);
45+
}
46+
# [END fs_collection_group_query]
47+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
2+
/**
3+
* Copyright 2020 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\FirestoreClient;
27+
28+
/**
29+
* Create example collection group for documents.
30+
* ```
31+
* collection_group_query_setup('your-project-id');
32+
* ```
33+
*/
34+
function collection_group_query_setup(string $projectId): void
35+
{
36+
// Create the Cloud Firestore client
37+
$db = new FirestoreClient([
38+
'projectId' => $projectId,
39+
]);
40+
41+
# [START fs_collection_group_query_data_setup]
42+
$citiesRef = $db->collection('cities');
43+
$citiesRef->document('SF')->collection('landmarks')->newDocument()->set([
44+
'name' => 'Golden Gate Bridge',
45+
'type' => 'bridge'
46+
]);
47+
$citiesRef->document('SF')->collection('landmarks')->newDocument()->set([
48+
'name' => 'Legion of Honor',
49+
'type' => 'museum'
50+
]);
51+
$citiesRef->document('LA')->collection('landmarks')->newDocument()->set([
52+
'name' => 'Griffith Park',
53+
'type' => 'park'
54+
]);
55+
$citiesRef->document('LA')->collection('landmarks')->newDocument()->set([
56+
'name' => 'The Getty',
57+
'type' => 'museum'
58+
]);
59+
$citiesRef->document('DC')->collection('landmarks')->newDocument()->set([
60+
'name' => 'Lincoln Memorial',
61+
'type' => 'memorial'
62+
]);
63+
$citiesRef->document('DC')->collection('landmarks')->newDocument()->set([
64+
'name' => 'National Air and Space Museum',
65+
'type' => 'museum'
66+
]);
67+
$citiesRef->document('TOK')->collection('landmarks')->newDocument()->set([
68+
'name' => 'Ueno Park',
69+
'type' => 'park'
70+
]);
71+
$citiesRef->document('TOK')->collection('landmarks')->newDocument()->set([
72+
'name' => 'National Museum of Nature and Science',
73+
'type' => 'museum'
74+
]);
75+
$citiesRef->document('BJ')->collection('landmarks')->newDocument()->set([
76+
'name' => 'Jingshan Park',
77+
'type' => 'park'
78+
]);
79+
$citiesRef->document('BJ')->collection('landmarks')->newDocument()->set([
80+
'name' => 'Beijing Ancient Observatory',
81+
'type' => 'museum'
82+
]);
83+
print('Added example landmarks collections to the cities collection.' . PHP_EOL);
84+
# [END fs_collection_group_query_data_setup]
85+
}

firestore/src/in_array_query.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
/**
3+
* Copyright 2020 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\FirestoreClient;
27+
28+
/**
29+
* Create a query with IN clause with array item.
30+
* ```
31+
* in_array_query('your-project-id');
32+
* ```
33+
*/
34+
function in_array_query(string $projectId): void
35+
{
36+
// Create the Cloud Firestore client
37+
$db = new FirestoreClient([
38+
'projectId' => $projectId,
39+
]);
40+
$citiesRef = $db->collection('cities');
41+
# [START fs_query_filter_in]
42+
$rangeQuery = $citiesRef->where('regions', 'in', [['west_coast'], ['east_coast']]);
43+
# [END fs_query_filter_in]
44+
foreach ($rangeQuery->documents() as $document) {
45+
printf('Document %s returned by query regions in [[west_coast], [east_coast]]' . PHP_EOL, $document->id());
46+
}
47+
}

firestore/src/in_query.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
/**
3+
* Copyright 2020 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\FirestoreClient;
27+
28+
/**
29+
* Create a query with IN clause.
30+
* ```
31+
* in_query('your-project-id');
32+
* ```
33+
*/
34+
function in_query(string $projectId): void
35+
{
36+
// Create the Cloud Firestore client
37+
$db = new FirestoreClient([
38+
'projectId' => $projectId,
39+
]);
40+
$citiesRef = $db->collection('cities');
41+
# [START fs_query_filter_in]
42+
$rangeQuery = $citiesRef->where('country', 'in', ['USA', 'Japan']);
43+
# [END fs_query_filter_in]
44+
foreach ($rangeQuery->documents() as $document) {
45+
printf('Document %s returned by query country in [USA, Japan]' . PHP_EOL, $document->id());
46+
}
47+
}

0 commit comments

Comments
 (0)