1

I'm trying to set a field value via API using podio-php. If the field was already not empty, the following snippet made according to the manual works fine:

$item = PodioItem::get_basic( $item_id );    
$field = $item->fields["field-name"];
$field->values = "2"; // let's say we have a number filed
$item->save(); // $field->save() also works

But if the field was empty, a warning Creating default object from empty value occures on save. No errors, no changes in the item. This is true for different types of fields. I assume, a field-object should be created from scratch, but didn't managed to find an info on this for different field types.

So, please, how to correctly set a value with podio-php when the field is empty? Thanks.

2 Answers2

3

I had the Same Problem and found this Gem..

PodioItem::update(210606, array('fields' => array(
"title" => "The API documentation is much more funny",
"business_value" => array('value' => 20000, "currency" => "EUR"),
"due_date" => array('start' => "2011-05-06 11:27:20", "end" => "2012-04-30 
10:44:20"),
));

I modified it so I can update individual fields. But this will update the field without that stupid error.

$FieldsArray = [];
$FieldsArray['encounter-status'] = "Draft";
$item = PodioItem::update($itemID, array('fields' => $FieldsArray));
2

May be there is a simplier way, but here is a workaround I've found. If the field was not empty, we just assign the new value. But if the field was empty, we need to create a new field object of the corresponding type (see the list below) with the same name as the existing field and add it to the field collection. The old empty field will be replaced with the new one.

In this example we will set the number field:

$field_name = "field-name";
$new_value = 999; 

$item = PodioItem::get_basic( $item_id );
$field = $item->fields[$field_name];

if ( count($field->values) ){ // if the field is not empty
    $field->values = $new_value;
} else { // if the field is empty
    $new_field = new PodioNumberItemField($field_name); // new field with the same(!) name
    $new_field->values = $new_value; 
    $item->fields[] = $new_field; // the new field will replace the exsisting (empty) one
}

$item->save();

Constructors for other field objects types (found in models/PodioItemField.php):

PodioTextItemField
PodioEmbedItemField
PodioLocationItemField
PodioDateItemField
PodioContactItemField
PodioAppItemField
PodioCategoryItemField
PodioImageItemField
PodioFileItemField
PodioNumberItemField
PodioProgressItemField
PodioDurationItemField
PodioCalculationItemField
PodioMoneyItemField
PodioPhoneItemField
PodioEmailItemField