我嘗試在數(shù)據(jù)庫中插入項目時使用 ConditionExpression,但它不起作用,當 Putitem() 函數(shù)運行時,php 腳本會中斷。
如果該項目不存在,我想插入該項目。
$response = $client->putItem(array( 'TableName' => 'tablename', 'Item' => array( 'serialNumber' => array('S' => 'test123'), 'deviceType' => array('S' => '1') ), 'ConditionExpression' => 'attribute_not_exists(serialNumber)' ));
我嘗試 var_dump $response,但代碼在上面的函數(shù)上中斷。
serialNumber 它是一個分區(qū)鍵,應(yīng)該按預(yù)期工作。
下面的代碼工作正常,但他用新值替換現(xiàn)有項目,這是我不希望發(fā)生的情況。
$response = $client->putItem(array( 'TableName' => 'tablename', 'Item' => array( 'serialNumber' => array('S' => 'test123'), 'deviceType' => array('S' => '1') ) ));
當您設(shè)置的條件計算結(jié)果為 false 時,預(yù)計您會返回 CondidtionCheckFailedException
。嘗試將代碼包裝在 try/catch
塊中,看看它是否按預(yù)期工作?
try { $response = $client->putItem(array( 'TableName' => 'tablename', 'Item' => array( 'serialNumber' => array('S' => 'test123'), 'deviceType' => array('S' => '1') ) )); } catch(Exception $e) { echo $e->getMessage(); }