错误:一个或多个参数值无效 - DynamoDb

时间:2015-03-26 09:43:12

标签: amazon-dynamodb

我正在尝试使用以下代码更新DynamoDb中的表。

$response = $client->updateItem(array(
    "TableName" => "PlayerInfo",
    "Key" => array(
        "PlayerId" => array('N' => '201503261435580358849074082'),
    ),
    "AttributeUpdates" => array(
        'PlayerPrice' => array(
            'N' => '5'
        ),
    ),
    "ReturnValues" => \Aws\DynamoDb\Enum\ReturnValue::ALL_NEW
));

print_r($response);

但是,错误会中断其执行。它说:

One or more parameter values were invalid: Only DELETE action is allowed 
when no attribute value is specified.

有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:5)

看起来请求的格式错过了'操作'和'价值'参数。例如。以下内容对我有用:

$response = $client->updateItem(array(
    "TableName" => "PlayerInfo",
    "Key" => array(
        "PlayerId" => array('N' => '201503261435580358849074082'),
    ),
    "ReturnValues" => \Aws\DynamoDb\Enum\ReturnValue::ALL_NEW,

    "AttributeUpdates" => array(
        'PlayerPrice' => array(
            'Action' => \Aws\DynamoDb\Enum\AttributeAction::PUT,
            'Value' => array('N' => '5'),
        )
    )
));
print_r($response);

您还可以使用UpdateExpression来实现相同的效果(UpdateExpressions也提供比AttributeUpdates更大的灵活性,因此通常建议使用它们):

$response = $client->updateItem(array(
    "TableName" => "PlayerInfo",
    "Key" => array(
        "PlayerId" => array('N' => '201503261435580358849074082'),
    ),
    "ReturnValues" => \Aws\DynamoDb\Enum\ReturnValue::ALL_NEW,

    "UpdateExpression" => "SET #pp = :val",
    "ExpressionAttributeNames" => array(
        "#pp" => "PlayerPrice",
    ),
    "ExpressionAttributeValues" => array(
        ':val' => array('N' => '5')
    )
));
print_r($response);