|
1 | 1 |
|
2 | 2 | /**
|
3 |
| - * Copyright 2018 Google Inc. |
| 3 | + * Copyright 2024 Google Inc. |
4 | 4 | *
|
5 | 5 | * Licensed under the Apache License, Version 2.0 (the "License");
|
6 | 6 | * you may not use this file except in compliance with the License.
|
|
26 | 26 | use Google\Cloud\Firestore\FirestoreClient;
|
27 | 27 |
|
28 | 28 | /**
|
29 |
| - * An example of an invalid range query. @see https://cloud.google.com/firestore/docs/query-data/queries#compound_queries |
| 29 | + * Example of a query with range and inequality filters on multiple fields. |
| 30 | + * @see https://cloud.google.com/firestore/docs/query-data/multiple-range-fields |
30 | 31 | *
|
31 | 32 | * @param string $projectId The Google Cloud Project ID
|
32 | 33 | */
|
33 |
| -function query_filter_range_invalid(string $projectId): void |
| 34 | +function query_filter_compound_multi_ineq(string $projectId): void |
34 | 35 | {
|
35 | 36 | // Create the Cloud Firestore client
|
36 | 37 | $db = new FirestoreClient([
|
37 | 38 | 'projectId' => $projectId,
|
38 | 39 | ]);
|
39 |
| - $citiesRef = $db->collection('samples/php/cities'); |
40 |
| - # [START firestore_query_filter_range_invalid] |
41 |
| - $invalidRangeQuery = $citiesRef |
42 |
| - ->where('state', '>=', 'CA') |
43 |
| - ->where('population', '>', 1000000); |
44 |
| - # [END firestore_query_filter_range_invalid] |
| 40 | + $collection = $db->collection('samples/php/users'); |
| 41 | + // Setup the data before querying for it |
| 42 | + $collection->document('person1')->set(['age' => 23, 'height' => 65]); |
| 43 | + $collection->document('person2')->set(['age' => 37, 'height' => 55]); |
| 44 | + $collection->document('person3')->set(['age' => 40, 'height' => 75]); |
| 45 | + $collection->document('person4')->set(['age' => 40, 'height' => 65]); |
45 | 46 |
|
46 |
| - // This will throw an exception |
47 |
| - $invalidRangeQuery->documents(); |
| 47 | + # [START firestore_query_filter_compound_multi_ineq] |
| 48 | + $chainedQuery = $collection |
| 49 | + ->where('age', '>', 35) |
| 50 | + ->where('height', '>', 60) |
| 51 | + ->where('height', '<', 70); |
| 52 | + # [END firestore_query_filter_compound_multi_ineq] |
| 53 | + foreach ($chainedQuery->documents() as $document) { |
| 54 | + printf( |
| 55 | + 'Document %s returned by age > 35 and height between 60 and 70' . PHP_EOL, |
| 56 | + $document->id() |
| 57 | + ); |
| 58 | + } |
48 | 59 | }
|
49 | 60 |
|
50 | 61 | // The following 2 lines are only needed to run the samples
|
|
0 commit comments