|
| 1 | +<?php |
| 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 | +} |
0 commit comments