Cloud Spanner Client - Class StructValue (1.61.0)

Reference documentation and code samples for the Cloud Spanner Client class StructValue.

Defines a struct parameter value with its fields.

This class is included to provide a fluent interface to build complex struct parameter values for your queries against Cloud Spanner. If your struct does or may include unnamed fields, or duplicate field names, you must use a StructValue. In most cases, however, an associative array may be used.

Please note that query results expressed as structs will not be returned using this class. Query results will always be expressed as a PHP array. This class is intended to be used to create complex struct values only.

If your query contains duplicate field names, it must be returned as a name/value pair as demonstrated in the example below using Result::RETURN_NAME_VALUE_PAIR.

Example:

use Google\Cloud\Spanner\Database;
use Google\Cloud\Spanner\SpannerClient;
use Google\Cloud\Spanner\Result;
use Google\Cloud\Spanner\StructType;
use Google\Cloud\Spanner\StructValue;

$spanner = new SpannerClient();
$database = $spanner->connect('my-instance', 'my-database');

$res = $database->execute('SELECT * FROM UNNEST(ARRAY(SELECT @structParam))', [
    'parameters' => [
        'structParam' => (new StructValue)
            ->add('foo', 'bar')
            ->add('foo', 2)
            ->addUnnamed('this field is unnamed')
    ],
    'types' => [
        'structParam' => (new StructType)
            ->add('foo', Database::TYPE_STRING)
            ->add('foo', Database::TYPE_INT64)
            ->addUnnamed(Database::TYPE_STRING)
    ]
])->rows(Result::RETURN_NAME_VALUE_PAIR)->current();

echo $res[0]['name'] . ': ' . $res[0]['value'] . PHP_EOL; // "foo: bar"
echo $res[1]['name'] . ': ' . $res[1]['value'] . PHP_EOL; // "foo: 2"
echo $res[2]['name'] . ': ' . $res[2]['value'] . PHP_EOL; // "2: this field is unnamed"

Namespace

Google \ Cloud \ Spanner

Methods

__construct

Parameter
NameDescription
values array[]

An array containing a field value. Each value must be of form [(string|null) $name, mixed $value].

add

Add a single value to the struct.

Example:

$structValue->add('firstName', 'John');
Parameters
NameDescription
name string|null

The struct field name.

value mixed

The struct field value.

Returns
TypeDescription
Google\Cloud\Spanner\StructValueThe current instance, for chaining additional struct values.

addUnnamed

Add an unnamed value to the struct.

Example:

$structValue->addUnnamed('John');
Parameter
NameDescription
value mixed

The struct field value.

Returns
TypeDescription
Google\Cloud\Spanner\StructValueThe current instance, for chaining additional struct values.

values

Get the list of values.

Returns
TypeDescription
array[]