|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2024 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/main/datastore/api/README.md |
| 22 | + */ |
| 23 | + |
| 24 | +namespace Google\Cloud\Samples\Datastore; |
| 25 | + |
| 26 | +use Google\Cloud\Datastore\DatastoreClient; |
| 27 | +use DateTime; |
| 28 | + |
| 29 | +/** |
| 30 | + * Example of a query with range and inequality filters on multiple fields. |
| 31 | + * @see https://cloud.google.com/datastore/docs/multiple-range-fields |
| 32 | + * |
| 33 | + * @param string $namespaceId |
| 34 | + */ |
| 35 | +function query_filter_compound_multi_ineq(string $namespaceId = null): void |
| 36 | +{ |
| 37 | + $datastore = new DatastoreClient(['namespaceId' => $namespaceId]); |
| 38 | + // [START datastore_query_filter_compound_multi_ineq] |
| 39 | + $query = $datastore->query() |
| 40 | + ->kind('Task') |
| 41 | + ->filter('priority', '>', 3) |
| 42 | + ->filter('created', '>', new DateTime('1990-01-01T00:00:00z')); |
| 43 | + // [END datastore_query_filter_compound_multi_ineq] |
| 44 | + $result = $datastore->runQuery($query); |
| 45 | + $found = false; |
| 46 | + foreach ($result as $entity) { |
| 47 | + $found = true; |
| 48 | + printf( |
| 49 | + 'Document %s returned by priority > 3 and created > 1990' . PHP_EOL, |
| 50 | + $entity->key() |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + if (!$found) { |
| 55 | + print("No records found.\n"); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +// The following 2 lines are only needed to run the samples |
| 60 | +require_once __DIR__ . '/../../../testing/sample_helpers.php'; |
| 61 | +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
0 commit comments