We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Updating an entry with php. How?

Development and Programming

chichilatte's avatar
chichilatte
43 posts
13 years ago
chichilatte's avatar chichilatte

Trying to update an entry’s data with php (in EE2.4). Seems like the way to do it is with api_channel_entries->update_entry(). In this example I’m changing the status…

$status = 'closed';
   $entry = array(
      'channel_id' => $channel_id
    , 'entry_id'  => $entry_id
    , 'title'  => $title
    , 'entry_date' => $entry_date
    , 'status'  => $status
   );

   $this->EE->load->library('api');
   $this->EE->api->instantiate(array('channel_entries', 'channel_fields'));
   $this->EE->api_channel_fields->fetch_custom_channel_fields();
   $this->EE->api_channel_entries->update_entry($entry_id, $entry);

Well, except that update_entry() doesn’t preserve fields not mentioned in the $entry argument. That is, it sets them to an empty string. Who on earth would want that functionality from an update function?

Does anybody have any idea how this might be done?It’s such an obvious ask, but i can’t seem to find an answer. Safecracker does it well enough, so there might be some clues in there. I’m thinking just grab all the damn fields from the database and sling them into the $entry array. But that can’t be the way the API’s designed, shurleyy. Sigh.

EDIT: Okay, for updating an entry’s status, i’ve now got…

function set_status_for_entry($status, $entry_id) {
   // Whitelist the possible statuses
   if (!in_array($status, array('open', 'closed')) || !is_numeric($entry_id))
    return FALSE;
   // Don't bother with api_channel_entries, it's useless. Just tinker with the database.
   $sql = $this->EE->db->update_string('exp_channel_titles', array('status'=>$status), "entry_id = '". $entry_id ."'");
   $result = $this->EE->db->query($sql);
   return $result==TRUE;
  }

Updating the status is all well and good, but still looking for a way to update a custom field properly. Hmm.

       
vosSavant's avatar
vosSavant
380 posts
13 years ago
vosSavant's avatar vosSavant

Hey chichilatte,

I was about to chime in and join you, but there is a user comment in the docs that fixes the issue of custom fields not saving. You just need to add this:

$this->EE->api_channel_fields->fetch_custom_channel_fields();

after this:

$this->EE->api->instantiate('channel_fields');

So no, they didn’t write the API to blank out unspecified fields 😉

       
chichilatte's avatar
chichilatte
43 posts
13 years ago
chichilatte's avatar chichilatte

Thanks vosSavant, but i must have seen that doc comment already, cos that line is already in the example code i gave! Fields still get destroyed, even with this. You get a different result?

       
vosSavant's avatar
vosSavant
380 posts
13 years ago
vosSavant's avatar vosSavant

Hah. Sorry, you’re right! I didn’t look closely at your example code.

I do get a different result - the fields save fine for me. Here is what I have:

// instantiate EE Channel Entries API
$this->EE->load->library('api');
$this->EE->api->instantiate('channel_entries');
$this->EE->api->instantiate('channel_fields');
$this->EE->api_channel_fields->fetch_custom_channel_fields();

// update entry
$data = array(
   'channel_id'  => 1,
   'title'       => $venue_name,
   'entry_date'  => time(),
   'status'      => 'In Progress',
   'field_id_1'  => $max_occupancy,
   'field_ft_1'  => 'none',
   'field_id_24' => $is_nonprofit,
   'field_ft_24' => 'none'
);

if ( $this->EE->api_channel_entries->update_entry($entry_id, $data) === FALSE ) {
   show_error('An error occurred while updating your entry.');
}

Maybe you should instantiate channel entries and channel fields separately, rather than as an array? I don’t know enough about the API but that’s the only significant difference I can see between what you have and what I have.

       
the3mus1can's avatar
the3mus1can
426 posts
13 years ago
the3mus1can's avatar the3mus1can

@chichilatte you are correct. It will set the value of any field not in the data array passed to the update_entry method to be an empty string. It is rather frustrating behavior and I hope that it is changed in the future. It works fine when it is used in the context of the CP when you are submitting an entry edit form, but for other custom development it is not the desired behavior.

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.