【发布时间】:2022-06-14 20:22:47
【问题描述】:
我在数据库中插入项目时尝试使用 ConditionExpression,但它不起作用,当 Putitem() 函数运行时 php 脚本中断。
如果他不存在,我想插入该项目。
$response = $client->putItem(array(
'TableName' => 'tablename',
'Item' => array(
'serialNumber' => array('S' => 'test123'),
'deviceType' => array('S' => '1')
),
'ConditionExpression' => 'attribute_not_exists(serialNumber)'
));
我尝试对 $response 进行 var_dump,但代码在上面的函数中中断。
serialNumber 它是一个分区键,应该可以按预期工作。
下面的代码运行良好,但他用新值替换了现有项目,这是我不希望发生的。
$response = $client->putItem(array(
'TableName' => 'tablename',
'Item' => array(
'serialNumber' => array('S' => 'test123'),
'deviceType' => array('S' => '1')
)
));
【问题讨论】:
-
您的第一个示例,即带有条件表达式的示例,如果该项目尚未在表中,它是否有效?
-
是的,如果属性不存在,它会正常插入。我正在尝试使用循环插入多个值,这就是为什么我需要响应为“条件请求失败”以警告用户有多少插入失败。
-
“代码中断”是什么意思?您收到错误消息吗?它说什么?要具体。
标签: php amazon-dynamodb