AWS SDK for PHP DynamoDB PutItem for non-empty variables only

I am inserting variables into AWS DynamoDB from the $ dbf array [$ a] ['variable1'], $ dbf [$ a] ['variable2'], $ dbf [$ a] ['variable3'] ...

for some $ a, only 'variable1' and 'variable2' are set, and for other $ a, all 'variable #' are set.

The code below will not work because nulls or unspecified variables are not allowed.

Is there a way in a loop just to try "PutItem" for variables that are set?

foreach($dbf as $day)
{
$result = $client->PutItem(array(
    'TableName' => 'AWIS',
    'Item' => array(
        'id' => array('S' =>  $day['id']),
        'date'=> array('S' => $day['date']),
       'max'=> array('N' => $day['max']),
       'min'=> array('N' => $day['min']),
       'pre'=> array('N' => $day['pre']),
       'max_soil_temp'=> array('N' => $day['max_soil_temp']),
       'min_soil_temp'=> array('N' => $day['min_soil_temp']),
       'evap'=> array('N' => $day['evap']),
       'veg_wetting'=> array('N' => $day['veg_wetting']),
       'solar_rad'=> array('N' => $day['solar_rad']),
       'ob_temp'=> array('N' => $day['ob_temp']),
       'adj_min'=> array('N' => $day['adj_min']),
       'chill_hours'=> array('N' => $day['chill_hours'])
                 ),
                           ));
}

      

+3


source to share


1 answer


You can do the following:

if ($yourValue) {
        $params['yourKey'] = array(
            'Action' => 'PUT',
            'Value' => array(
                Type::STRING => $yourValue
            )
        );
    }

$response = $Client->updateItem(array(
            "TableName" => "tableName",
            "Key" => array(
                "Id" => array(
                    Type::STRING => $Id
                )
            ),
            "AttributeUpdates" => $params
                )
        ); 

      



Set all parameters available in the loop and update only those values ​​that are available

Hope it helps

+1


source







All Articles