0

I've looked at the EE Channel Entries API docs, but the save_entry function does not seem to actually update an existing entry, but rather saves a new new entry over it. This seems to be the case as any fields not specified in the $data array get wiped out and the entry_date is changed to the date the update was made.

Is there a way to simply edit/update an entry without the entry_date being changed, yet still triggering the edit notice in the EE system? I am able to update single fields using the codeigniter db class, but doing it this way does not trigger EE's system to notice that the entry has been edited.

The main reason I want to do this is to use Postmaster's email trigger on entry edit, but doing a manual db update will not trigger this. My plugin code for the channel entries API update is below:

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

$data = array(
     'title' => $some_title,
     'url_title' => $some_url_title,
     'field_id_17' => $some_variable,
);

ee()->api_channel_entries->save_entry($data, 4, $entry_id, FALSE);

/*
 * The number 4 in the parameters is the channel_id number where the entry is stored.
 * I have also tried using NULL in place of 4 but it then throws an error saying
 * I'm missing an API parameter.
 */

I would also not mind it if I could just use the codeigniter db class to update the entries, then somehow tell the EE system that the entry has been updated. Is this sort of thing possible?

  • Are you sure that $entry_id is populated and correct? – Derek Hogue Apr 21 '14 at 11:53
  • Yes, I've tested the code using the same $entry_id variable but with the codeigniter db class update function instead and it works as intended. Just to be clear, the above script does create an entry, but it replaces the entry_date with the current time. I know I can also pull the old data from the db and repopulate all the fields, but if possible I'd like to avoid doing so because it means a lot of recoding on my part. – positlux Apr 21 '14 at 12:14
  • I guess from previous things I've read that the save_entry function just works like that... it simply replaces all the fields and requires repopulating all fields when using it to 'update' an entry. So I suppose what I'm really after now is a way to tell EE that an entry has been edited without using the save_entry function. – positlux Apr 21 '14 at 12:51
1

I know it seems redundant, but in the Channel Entries API you need to pass the entry_id in the data array. EE looks to that array to determine if the entry is being edit correctly (as far as the hooks go). I have tested this and confirmed it a while back, so I assume it's still true. Without the entry_id in the data array, the entry should get updated but in the hooks there is no way to tell if the entry is being edited without the entry_id passed in the array. If you look through the EE core, I believe this is what they do as well.

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

$data = array(
    'entry_id' => $entry_id,
    'title' => $some_title,
    'url_title' => $some_url_title,
    'field_id_17' => $some_variable,
);

ee()->api_channel_entries->save_entry($data, 4, $entry_id, FALSE);

And to further confirm... You need to pass all the values in the data array or they will get wiped out as you suggested. This is really limiting of the Channel Entries API and should be updated. If you want a robust example of how I integrate with the Channel Entries API, take a look at the new version of Channel Data. https://github.com/objectivehtml/Channel-Data

| improve this answer | |
  • I appreciate the help and the link to your Channel Data code. It's too bad that the current Channel Entries API is somewhat limited in this way, but I guess we gotta work with what we've got! – positlux Apr 22 '14 at 7:57
0

OK, I've decided to bite the bullet and update my code in various places to trigger notification that the entry has been updated. For anyone who's interested and who is similarly facing this sort of issue, I'm using the following code in my plugin to accomplish this. My db query is selecting all because I have several different channels with dozens of unique fields each to account for, so this is saving me a lot of time in coding out each entry and in potentially missing some. However, I do worry about if this will cause performance issues down the line. I'm pretty new to programming in general, so of course if anyone sees a better way of doing this or thinks that my code is awful in some way please do let me know!

/**
 * First pull the old entry info.  I'm selecting all here because less code
 * and less chance of me missing a field and having it get wiped.
 */ 

$edit_results = ee()->db->select()
    ->from ('channel_data c')
    ->join ('channel_titles t', 'c.entry_id = t.entry_id')
    ->where (array(
        't.entry_id' => $entry_id,
        ))
    ->get();

/**
 *  I am unsetting the edit_date field so that the correct edit time gets posted.
 *  This particular update is for just updating the status of an entry.
 *  Any other fields to update can also be included below.
 */

if ($edit_results->num_rows() > 0)
{
    foreach($edit_results->result_array() as $row)
    {
        $channel_id = $row['channel_id'];
        $edit_array = $row;
        $edit_array['status'] = $status;
        unset($edit_array['edit_date']);
    }
}

ee()->load->library('api');
ee()->api->instantiate('channel_entries');
ee()->api_channel_entries->save_entry($edit_array, $channel_id, $entry_id, FALSE);
| improve this answer | |

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.