PodioItem::Update - updating an Embed Field...
AnsweredI am reviewing the sample code in /examples/items.php, and I don't see examples of how to set some of the more complex PodioItemFields such as Embed fields and Reference fields...
Would it be possible to have this updated with examples of every PodioItemField type?
I am speaking specifically about how to work to call set_value for these field types...
For example, I am trying to update the value of an "embed" field (a link field) and I am calling code similar to:
$attributes = array( 'url' => 'http://www.infranet.com' );
$embed = PodioEmbed::create( $attributes );
$this->orgItem->field( 'organizationlink' )->set_value( $embed->embed_id );
(I also tried
$this->orgItem->field( 'organizationlink' )->set_value( $embed );
)
The creation of the "embed" is working fine... I get this error on the set_value() call...
If the PodioItem I am trying to update has no value in the 'organizationlink' field, I get this error:
Call to a member function set_value() on a non-object
This is happening because the orgItem that was returned from Podio when I called PodioItem::filter didn't contain any value for the 'organizationlink' field...
Why doesn't set_value() take care of this and create the new 'field'? What do I have to do to update this field and then what is proper way to embed a link in the embed field type?
Thanks
Patrick Steil
Patrick Steil
-
Hi Patrick,
->set_value() can't actually take a PodioEmbed object as an argument. I should probably make that work...
Anyway, your problem is that you have to create an embed before you can attach it to an item. Conceptually it works the same as files. You must first upload the file/create the embed to get a file_id/embed_id and then use that id when setting the item field value.
So in your case:
// Create the embed:
$embed = PodioEmbed::create(array('url' => 'http://example.com/'));// Pretend $item holds a PodioItem instance
$item->field('organizationlink')->set_value(array('embed' => $embed->id, 'file' => $embed->files[0]->));// I'm not actually sure you can use that shorthand for grabbing the file_id of the first file of the embed, but I'll let you figure that out :)
It's on my very long list of things to do to create more comprehensive tutorials for working with items and item fields, but there's just been so many other things. I hope you like the new navigation and chat :)
/Andreas
-
Ok, I got this to work finally:
$attributes = array( 'url' => 'http://www.infranet.com' ); $embed = PodioEmbed::create( $attributes ); $attribute['embed']['embed\_id'] = $embed->embed\_id; $attribute['file']['file\_id'] = $embed->files[0]->file\_id; $this->orgItem->field('organizationlink')->set\_value($attribute);
I feel like I just cheated and created my own data structure to meet the needs of the set_value() code... I wish I could have just passed in the $embed object like this:
$attributes = array( 'url' => 'http://www.infranet.com' ); $embed = PodioEmbed::create( $attributes ); $this->orgItem->field('organizationlink')->set\_value($embed);
Can set_value() be enhanced to support this? Would make a lot more sense... :) I tried to get it to do that, but didn't understand enough about the PodioItemField class to be able to get it to work...
Now my other bigger problem is that the above code ONLY works if the "organizationlink" field ALREADY has an "embed" object specified in the Podio Item... so in effect my code above works as an "update" to that field, but if I remove the "embed" object from the Item and then run my code I get this error:
**Fatal error**: Call to a member function set\_value() on a non-object in **C:\git\podiohelpdesk\helpdesk\libs\PTOrganization.class.php** on line **292** **```** which is pointing to this line of code:
$this->orgItem->field('organizationlink')->set_value($attribute);
I think this is something that set\_value() should take care of... ? Thanks...
-
Sorry, was trying to figure out how to use "block quotes" in Zen markdown... according to their site I used the same character they said to use... is it supposed to be a single quote? I used the angled quote that is on the key next to the "1" key... which is what they showed on their website: https://support.zendesk.com/entries/21714462
-
Hi Patrick,
I made some changes to podio-php that should make your life a bit easier. Easy part first: When you have a PodioItem and you want to add a new field to it that didn't previously have a value. You then need to 1) Create a new PodioItemField and 2) add that object to the fields collection. It was a little annoying to write the example so instead I wrote some convenience methods for adding and removing fields. See example at https://github.com/podio/podio-php/blob/master/examples/items.php#L110-L112 (see the highlighted lines)
Secondly, this sample of yours:
$attributes = array( 'url' => ' http://www.infranet.com' );
$embed = PodioEmbed::create( $attributes );
$this->orgItem->field('organizationlink')->set_value($embed);Should work now if you pull a fresh copy of podio-php. It's only for embed fields right now but I'll get it done in a similar way for contact, app, image etc. fields as well.
/Andreas
-
Andreas,
I have now been able to update the "organizationlink" field as I desired, thanks!
A side issue that popped up:
I am not sure if I missed that I was getting this error before ( This code was working before) but now when I do the following:
$field = new PodioAppItemField($field_id);
$field->set_value($reference_to_item_id); // The item to add a reference to
$obj = PodioItemField::update($item_id, $field_id, $field->as_json(false));
on the set_value() call I am getting this error:
Warning: Cannot use a scalar value as an array in C:\git\podiohelpdesk\helpdesk\libs\PodioApiV3\models\PodioItemField.php on line 79I report it here only because maybe this is a side effect of the changes you made last night?
I am trying to set an app reference... also, should there be an PodioAppReferenceItemField class?
-
Nothing changed in that part of the code so you would have gotten the error before my latest changes as well. You have probably only been passing in blank values so far. Anyway, wrap the argument in an array:
$field->set_value(array($reference_to_item_id));
Same for contact fields: Pass in an array of profile ids
Please sign in to leave a comment.
Comments
10 comments