Reputation: 148
I am trying to create new items in four apps apps via one form -- some the items will become app references. For example, I first create an Entity item, then a Person item and relate the new Entity to the Person via an app reference field in the Person App. Then create a Project and relate both the Person and the Entity to the Project. This was successful until I upgraded to the latest Podio-PHP. In the past, when newly created items were returning item_id as integer then I could pass that into the field as an app reference before the next item was created.
I've been trying to get the item_id as integer so I can do the same, but I keep getting null. Anyway, given the way Podio now returns newly created items as objects, should I be trying this a different way?
$app_id = 1234567;
$fields = new PodioItemFieldCollection(array(
...I am defining external_id and values...
));
$item = new PodioItem(array(
'app' => new PodioApp($app_id),
'fields' => $fields,
));
if( $files ){
$item->files = new PodioCollection($files);
};
$item->save();
$new_id = $item->item_id;
return $new_id;
$new_id is where I'm trying to store the integer so I can pass it along to the next item creation as an app reference.
Upvotes: 0
Views: 597
Reputation: 752
It's not really handled well in podio-php (it's been handled equally un-well since forever though). If you look at the save
method you can see what's going on: https://github.com/podio/podio-php/blob/master/models/PodioItem.php#L58-L72
public function save($options = array()) {
$json_attributes = $this->as_json_without_readonly_fields();
if ($this->id) {
return self::update($this->id, $json_attributes, $options);
}
else {
if ($this->app && $this->app->id) {
return self::create($this->app->id, $json_attributes, $options);
}
else {
throw new PodioMissingRelationshipError('{"error_description":"Item is missing relationship to app"}', null, null);
}
}
}
save()
doesn't assign the new item_id to the object itself. This is a bug and it would be good if you created an issue at https://github.com/podio/podio-php/issues so it can be fixed.
For now you can see that save()
returns the same as the static create
method. So your last three lines needs to be replaced with:
$new_item_placeholder = $item->save();
$item->item_id = $new_item_placeholder->item_id;
return $item->item_id;
Upvotes: 1