Lamplight API examples - editing profiles

Introduction

Version 1.2 of the Lamplight API allows you to create and edit profiles for people or organisations. This example shows you how to edit an existing profile.

Example

In this example, we're going to fetch details of a particular organisation, get the value of a numerical field, increase it by one, and save that value back to Lamplight. The code used in this example is mingled with the explanation and results, unlike other examples. Below each code sample is the actual output of the code above.

We already know the ID of the organisation that is published and alterable via the API, so first of all we use fetchOne() on the Lamplight_Client and get the profile as an instance of Lamplight_Record_Orgs. This is based on this example when you click on a link for more details.

    $resp = $client->fetchOrgs("user")
        ->fetchOne()
        ->setParameterGet('id', 31)
        ->request();

    $rec = false;

    // check the response first:
    if (!$resp->isError() && $resp->getStatus() == 200) {

        // Try and get the record
        $recs = $client->getRecordSet();
        if ($recs && $recs->count() == 1) {
            $rec = $recs->current();
        }

    }

    // Tell them what's happening:
    if (!$rec) {
        echo '

There was a problem retrieving the record

'; } else { echo '

The name of the organisation we are going to update is: ' . htmlentities($rec->get('name')) . '

'; }

There was a problem retrieving the record

So we hopefully now have an organisation. The field we are going to view and update is a numeric custom field called 'Number of example views'. Let's see what it is now:


    if ($rec) {

        $numViews = $rec->get('Number_of_example_views');
        echo '

The number of example views is currently ' . $numViews . '

'; } else { echo '

We do not know this organisation...

'; }

We do not know this organisation...

So now we'd like to increment this and save it to Lamplight. This is straightforward: we set the value and then use the Lamplight_Client to save() the record. The only thing to remember here is to resetClient before we save the record.



    if ($rec) {

        $rec->set('Number_of_example_views', $numViews + 1);
        $client->resetClient();
        $saveResponse = $client->save($rec);
    
        if (!$saveResponse->isError() && $saveResponse->getStatus() == 200) {    

            echo '

Successfully updated the profile.

'; var_dump($saveResponse->getBody()); } else { echo '

There was a problem updating the profile

'; } } else { echo '

We still do not know this organisation and cannot update.

'; }

We still do not know this organisation and cannot update.

Finally, we'll request the profile once more to view the updated value.


    $client->resetClient();
    $resp = $client->fetchOrgs("user")
        ->fetchOne()
        ->setParameterGet('id', 31)
        ->request();

    $rec = false;

    if (!$resp->isError() && $resp->getStatus() == 200) {

        $recs = $client->getRecordSet();
        if ($recs && $recs->count() == 1) {
            $rec = $recs->current();
        }

    }


    if (!$rec) {
        echo '

There was a problem retrieving the record

'; } else { echo '

The name of the organisation we are going to update is: ' . htmlentities($rec->get('name')) . ' and the number of example views is now ' . $rec->get('Number_of_example_views') . '

'; }

There was a problem retrieving the record