Podio API:如何使用 podio-php 包装器为空字段设置值?
Podio API: how to set a value for an empty field with podio-php wrapper?
我正在尝试使用 podio-php. If the field was already not empty, the following snippet made according to the manual 通过 API 设置字段值,效果很好:
$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
但如果字段 为空 ,则保存时会出现警告 Creating default object from empty value
。没有错误,项目没有变化。这适用于不同类型的字段。我想,应该从头开始创建一个字段对象,但没有找到关于不同字段类型的信息。
那么,请问如何在字段为空时使用 podio-php 正确设置值?谢谢。
可能有更简单的方法,但这是我找到的解决方法。如果该字段不为空,我们只需分配新值。但是如果该字段为空,我们需要创建一个与现有字段同名的相应类型(见下表)的新字段对象,并将其添加到字段集合中。旧的空白字段将被新字段替换。
在此示例中,我们将设置数字字段:
$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();
其他字段对象类型的构造函数(在 models/PodioItemField.php 中找到):
PodioTextItemField
PodioEmbedItemField
PodioLocationItemField
PodioDateItemField
PodioContactItemField
PodioAppItemField
PodioCategoryItemField
PodioImageItemField
PodioFileItemField
PodioNumberItemField
PodioProgressItemField
PodioDurationItemField
PodioCalculationItemField
PodioMoneyItemField
PodioPhoneItemField
PodioEmailItemField
我遇到了同样的问题并发现了这个 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"),
));
我修改了它以便我可以更新各个字段。但这将更新字段而不会出现那个愚蠢的错误。
$FieldsArray = [];
$FieldsArray['encounter-status'] = "Draft";
$item = PodioItem::update($itemID, array('fields' => $FieldsArray));
我正在尝试使用 podio-php. If the field was already not empty, the following snippet made according to the manual 通过 API 设置字段值,效果很好:
$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
但如果字段 为空 ,则保存时会出现警告 Creating default object from empty value
。没有错误,项目没有变化。这适用于不同类型的字段。我想,应该从头开始创建一个字段对象,但没有找到关于不同字段类型的信息。
那么,请问如何在字段为空时使用 podio-php 正确设置值?谢谢。
可能有更简单的方法,但这是我找到的解决方法。如果该字段不为空,我们只需分配新值。但是如果该字段为空,我们需要创建一个与现有字段同名的相应类型(见下表)的新字段对象,并将其添加到字段集合中。旧的空白字段将被新字段替换。
在此示例中,我们将设置数字字段:
$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();
其他字段对象类型的构造函数(在 models/PodioItemField.php 中找到):
PodioTextItemField
PodioEmbedItemField
PodioLocationItemField
PodioDateItemField
PodioContactItemField
PodioAppItemField
PodioCategoryItemField
PodioImageItemField
PodioFileItemField
PodioNumberItemField
PodioProgressItemField
PodioDurationItemField
PodioCalculationItemField
PodioMoneyItemField
PodioPhoneItemField
PodioEmailItemField
我遇到了同样的问题并发现了这个 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"),
));
我修改了它以便我可以更新各个字段。但这将更新字段而不会出现那个愚蠢的错误。
$FieldsArray = [];
$FieldsArray['encounter-status'] = "Draft";
$item = PodioItem::update($itemID, array('fields' => $FieldsArray));