1

Using the Expression Engine Channel Entries API, is it possible to only update 1 field?

Using the below code, it appears you have to grab all the field data and then re-add it to the fields.

Is it not possible to do the below, just updating a single field (field_id_306).

ee()->load->library('api');
ee()->legacy_api->instantiate('channel_entries');

$query = ee()->db->select('title, url_title')
        ->from('exp_channel_titles')
        ->where('entry_id', $entry_id)
        ->get();

if ( $query->num_rows() > 0 )
{
    $row = $query->row();

    $data = array(
        'entry_id' => $entry_id,
        'title' => $row->title,
        'url_title' => $row->url_title,
        'field_id_306' => '10'
    );

    ee()->api_channel_entries->save_entry($data, $channel_id, $entry_id, FALSE);
}
1

It certainly is!

Here's what you need to do (From the manual) based on your example :

ee()->load->library('api');
ee()->api->instantiate('channel_entries');
ee()->api->instantiate('channel_fields');

//get the row to edit
$query = ee()->db->select('title, url_title')
    ->from('exp_channel_titles')
    ->where('entry_id', $entry_id)
    ->get();

if ( $query->num_rows() > 0 )
{
    $row = $query->row();

    $data = array(
        'entry_id' => $entry_id,
        'title' => $row->title,
        'url_title' => $row->url_title,
        'field_id_306' => '10'
    );

    ee()->api_channel_fields->setup_entry_settings($channel_id, $data);

    $success = ee()->api_channel_entries->save_entry($data, $channel_id, $entry_id, FALSE);

    if ( ! $success)
    {
        show_error(implode('<br />', $this->api_channel_entries->errors));
    }

Under the hood, you'll notice the main difference in loading the channel_fields library and the call to setup_entry_settings prior to the save_entry call.

It's quite convluted waht happens in setup_entry_settings but essentually it loads all the fields for the channel_data table, I beleive it then then sets them to their existing value, or the default value, if they arn't present in the $data array, all under the hood.

Ref : https://docs.expressionengine.com/v2/development/api/api_channel_fields.html#Api_channel_fields::setup_entry_settings

Ref : https://ellislab.com/forums/viewthread/168319/#819749

Disclaimer : I have been using this lately in the context of create_new_entry but the logic is the same I understand.

Scratch that Ok, I've just gone ahead and actually read the function, it doesn't look like it does actually load the existing values however looking at update_entry (which is called from save_entry, it only builds the query using the columns in your data array, so as long as your entry_id is set correctly (int the $data as well as in the save_entry parameter) it should update properly.

But please do let me know if it doesn't (with the channel_fields part) please let me kno and I will adjust my understanding accordinly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.