Lion Kuo
Lion Kuo

Reputation: 239

DynamoDB PHP Return Value

I am studying how to write php code of DynamoDB now.

I have encounter some problems.

1) How can I know when connect to db fail

$aws = Aws::factory('config.php');
$client = $aws->get('DynamoDb');

Return client is a object, but i don't know how to check connect success or fail in my php code.

2) Same question when i try to putItem, updateItem and deleteItem

$result = $client->putItem(array(
        ...
    ));

I read the AWS SDK of PHP document, but I can't find solution.

These function return value is a array(?) and no attribute is mean success or fail.

Only queryItem() can check by 'Count' attribute.

How should I do in my php code to check these ?

Thanks in advance.

Upvotes: 2

Views: 1297

Answers (1)

Jeremy Lindblom
Jeremy Lindblom

Reputation: 6527

Regarding #1, the "connection" should not be treated the same way as a connection to a MySQL database. Requests to DynamoDB are made over HTTP(S), and this does not require that you establish an upfront connection. When you create the client object, you are not making a connection to DynamoDB, you are just configuring an HTTP client that will make requests to DynamoDB.

Regarding #2, I think you should read the SDK's Getting Started Guide, especially the sections on Working with modeled responses and Detecting and handling errors. Basically, if a request succeeds you get a Guzzle\Service\Resource\Model object, which behaves like an array (i.e., implements PHP's ArrayAccess and Traversable interfaces). If the request fails, an exception is thrown. For DynamoDB, that exception will be Aws\DynamoDb\Exception\DynamoDbException.

try {
    $result = $client->putItem(array(
        'Table' => 'my-table',
        // ...
    ));
} catch (DynamoDbException $e) {
    // The PutItem operation failed.
    echo $e->getMessage();
}

Upvotes: 2

Related Questions