簡體   English   中英

Podio API:如何使用podio-php包裝器為空字段設置值?

[英]Podio API: how to set a value for an empty field with podio-php wrapper?

我正在嘗試使用podio-php通過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正確設置值? 謝謝。

我遇到了同樣的問題,找到了這顆寶石。

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));

可能有一種更簡單的方法,但是這是我找到的解決方法。 如果該字段不為空,則只需分配新值。 但是,如果該字段為空,則需要創建一個與現有字段同名的對應類型的新字段對象(請參見下面的列表),並將其添加到字段集合中。 舊的空字段將被新的字段替換。

在此示例中,我們將設置數字字段:

$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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM