trrisner
trrisner

Reputation: 21

AWS SDK for PHP DynamoDB PutItem for non null variables only

I'm inserting variables into the AWS DynamoDB from an array $dbf[$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 below code won't work because Null or non set variables aren't allowed.

Is there a way in the loop to only attempt to "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'])
                 ),
                           ));
}

Upvotes: 2

Views: 364

Answers (1)

Harshal Bulsara
Harshal Bulsara

Reputation: 8254

You can potentially do like:

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 the params that are available in loop and will update only those values which is available

Hope that helps

Upvotes: 1

Related Questions