使用 AWS 开发工具包 PHP 的 DynamoDB 中的保留字

Ber*_*lin 2 php amazon-web-services amazon-dynamodb

我正在使用batchGetItem方法来检索 x 个项目,并使用它ProjectionExpression来检索每个项目的 x 个属性。

$result = $client->batchGetItem(array(
    'RequestItems' => array(
        $table => array(
            'Keys' => $keys,
            'ConsistentRead' => true,
            'ProjectionExpression' => "id, name"
        ),
    ),
));
$read_items = $result->getPath("Responses/{$table}");
Run Code Online (Sandbox Code Playgroud)

然而,我的一些项目具有诸如name和 之类的属性token,这些属性是 DynamoDB 中的保留字。

http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ReservedWords.html

{"__type":"com.amazon.coral.validate#ValidationException","message":"Invalid ProjectionExpression: Attribute name is a r (truncated...)
 ValidationException (client): Invalid ProjectionExpression: Attribute name is a reserved keyword; reserved keyword: name - {"__type":"com.amazon.coral.validate#ValidationException","message":"Invalid ProjectionExpression: Attribute name is a reserved keyword; reserved keyword: name"}
Filename:    /var/app/vendor/aws/aws-sdk-php/src/WrappedHttpHandler.php
Run Code Online (Sandbox Code Playgroud)

据我所知,expression attribute name这是这样做的灵魂,但我正在努力寻找实现它的方法batchGetItem,有人可以举一个小例子,说明如何使用 SDK PHP 版本 3.20.11 来实现它吗?

not*_*est 5

这是一个示例ExpressionAttributeNames

“#name” - 是属性名称的占位符,实际的属性名称将替换为 中的值ExpressionAttributeNames

$result = $client->batchGetItem(array(
    'RequestItems' => array(
        $table => array(
            'Keys' => $keys,
            'ConsistentRead' => true,
            'ProjectionExpression' => "id, #name",
            'ExpressionAttributeNames' => array('#name' => 'name'}
        ),
    ),
));
Run Code Online (Sandbox Code Playgroud)

参考链接