Forum

Thread tagged as: Question, Api

Updating an existing Collection item

Hi, I've been looking through the Collections API details but I can't see how to do this - if an item already exists, instead of adding an item, I'd like to modify the existing one. How can I do that?

Matt Keehan

Matt Keehan 0 points

  • 3 years ago

Matt, you should be able to first click "Pages", then click "Collections" click the collection where your content is stored, then from the list of Collection Items you schould be able to click on the "Title" of the item and this will put you into "Edit" mode for that item.

The process is nearly exact to editing a page or page region.

Thanks Robert! I was trying to figure out how to do it through code, rather than the interface - can you help?? :)

Drew McLellan

Drew McLellan 2638 points
Perch Support

What's the use case you're trying to address?

We're using the API to import items in to a Collection. If an item already exists (identified by a required field), we need to update the existing item.

Drew McLellan

Drew McLellan 2638 points
Perch Support

I don't think the import API has a mechanism for updating.

No, it doesn't appear to :(

Can you help me figure out how to do it?

Drew McLellan

Drew McLellan 2638 points
Perch Support

I don't think there's a way to do it currently.

I'll leave this here for posterity, in case anyone comes across the same issue - I managed to do it in the end by matching the existing item like:

$matchingItem = perch_collection('CollectionName', [
    'count' => 1,
    'filter' => 'a_collection_fieldname',
    'match' => 'eq',
    'value' => $value,
    'skip-template' => true
    ], true);

Then if I get a match, I grab any existing data I need to keep, and add it to the new record I'm importing. Then I delete the old item with

$Collections = new PerchContent_Collections;
$Collection = $Collections->get_one_by('collectionKey', 'CollectionName');
$Collection->delete_item($matchingItem['_id']);

Seems to work merrily :)

Drew McLellan

Drew McLellan 2638 points
Perch Support

As you're using a private API we can't guarantee that this will keep working.