2

EE 5.2.4

I am using the ee model class to save channel entries via AJAX.

Everything is working well except for custom fields, which don't save.

Here is the code:

      $entry = ee('Model')->make('ChannelEntry');
      $entry->title = $title;
      $entry->url_title = $url_title;
      $entry->site_id =  ee()->config->item('site_id');
      $entry->author_id = ee()->session->userdata('member_id');
      $entry->ip_address = ee()->session->userdata['ip_address'];
      $entry->entry_date = ee()->localize->now;
      $entry->edit_date = ee()->localize->now;
      $entry->custom_field = $custom_field_value;
      $entry->channel_id = $channel_id;
      $entry->save();

Everything works except for the custom_field part. It will create the new entry and save all of the correct data, but just not that.

Is there a step I'm missing?

1 Answer 1

3

You may be using the incorrect field name. You can't access it via its short name, it has to be by it's field_id. Here is an example of how I do something like this:

// these are usually set at the top of my add-on, function, whatever, 
// for reference through out this PHP file, and also in case different site
// instances (staging, dev, whatever) happen to have different field_ids
// for the same fields, so it's easier to change when pushing around
$first_name_field_id = "field_id_10";
$last_name_field_id  = "field_id_20";

// ...

// then later, after instantiating the Channel Entry model, you apply field values like so:
$entry->$first_name_field_id = "Eric";
$entry->$last_name_field_id  = "Idle";
2
  • 1
    That's it, man! You're a life-saver, as usual. Thanks so much!
    – Shayne
    Commented Aug 20, 2019 at 23:23
  • Always glad to help!
    – jrothafer
    Commented Aug 20, 2019 at 23:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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