|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2025 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/spanner/README.md |
| 22 | + */ |
| 23 | + |
| 24 | +namespace Google\Cloud\Samples\Spanner; |
| 25 | + |
| 26 | +use Google\Cloud\Spanner\SpannerClient; |
| 27 | +use Google\Cloud\Spanner\Database; |
| 28 | +use Google\Cloud\Spanner\Proto; |
| 29 | +use Google\Cloud\Spanner\StructType; |
| 30 | +use Google\Cloud\Spanner\StructValue; |
| 31 | +use Testing\Data\User; |
| 32 | +use Testing\Data\Book; |
| 33 | + |
| 34 | +/** |
| 35 | + * Queries sample data from the database using proto columns. |
| 36 | + * Example: |
| 37 | + * ``` |
| 38 | + * query_data_with_struct_proto_columns($instanceId, $databaseId, $userId); |
| 39 | + * ``` |
| 40 | + * |
| 41 | + * @param string $instanceId The Spanner instance ID. |
| 42 | + * @param string $databaseId The Spanner database ID. |
| 43 | + * @param int $userId The ID of the user to query. |
| 44 | + */ |
| 45 | +function query_data_with_struct_proto_columns( |
| 46 | + string $instanceId, |
| 47 | + string $databaseId, |
| 48 | + int $userId = 1 |
| 49 | +): void { |
| 50 | + // [START spanner_query_data_with_struct_proto_columns] |
| 51 | + $spanner = new SpannerClient(); |
| 52 | + $database = $spanner->instance($instanceId)->database($databaseId); |
| 53 | + |
| 54 | + $structType = (new StructType) |
| 55 | + ->add('title', Database::TYPE_STRING) |
| 56 | + ->add('author', Database::TYPE_PROTO); |
| 57 | + |
| 58 | + $results = $database->execute( |
| 59 | + 'SELECT u.Id, u.User.name, ' . |
| 60 | + 'ARRAY(SELECT AS STRUCT b.title, b.author ' . |
| 61 | + 'FROM u.Books AS b) as book_struct '. |
| 62 | + 'FROM Users AS u' |
| 63 | + ); |
| 64 | + foreach ($results as $row) { |
| 65 | + // Print the decoded Protobuf message as JSON |
| 66 | + printf('User name: %s' . PHP_EOL, $row['name']); |
| 67 | + foreach ($row['book_struct'] as $bookStruct) { |
| 68 | + if (!class_exists(Book::class, false)) { |
| 69 | + // If you receive an error such as |
| 70 | + // "Unable to decode proto value. Descriptor not found for testing.data.User" |
| 71 | + // you may need to initialize the generated classes. This also happens when creating |
| 72 | + // an instance of the message class (e.g. `new User()`). |
| 73 | + \GPBMetadata\Data\User::initOnce(); |
| 74 | + } |
| 75 | + printf('Book struct title: %s' . PHP_EOL, $bookStruct['title']); |
| 76 | + printf('Book struct author: %s' . PHP_EOL, $bookStruct['author']->get()->serializeToJsonString()); |
| 77 | + } |
| 78 | + } |
| 79 | + // [END spanner_query_data_with_struct_proto_columns] |
| 80 | +} |
| 81 | + |
| 82 | +// The following 2 lines are only needed to run the samples |
| 83 | +require_once __DIR__ . '/../../testing/sample_helpers.php'; |
| 84 | +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
0 commit comments