1

Is it possible to use the model service to create a new entry?

I have been using the model service recently to update and delete channel entries, but can't figure out how to create.

$update_query = ee('Model')->get('ChannelEntry', $id_to_update)
                           ->first();

$update_query->set(array(
    'field_id_x' => 'Content',
    'field_id_x' => 'Content'
));
$update_query->save();
2

Yes, obviously it is possible to use the model to create a new entry. To create a new entry using model you should follow below syntax.

$entry = ee('Model')->make('ChannelEntry');
$entry->Channel = $channel;
$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->versioning_enabled = $channel->enable_versioning;
$entry->sticky = FALSE;
$entry->set($entryFieldArray);           /* Array of entry_data */
$entry->title = $title;
$entry->url_title = $url_title;
$entry->entry_date = ee()->localize->now;
$entry->edit_date = ee()->localize->now;
$entry->save();

This way by using ee('Model')->make('ChannelEntry') you can create new entry. I hope this will works for you.

| improve this answer | |
  • Thanks for your reply Harsh. I have been trying to get this to work. I keep getting: "Undefined variable: channel". I tried changing '$entry->Channel' to '$entry->channel' but still the same. Any ideas? – ccdavies Sep 28 '18 at 7:59
  • Here $channel means Channel ID. All other respective variables you need to change according to it's usage like $title and $url_title. – Harsh Barach Sep 28 '18 at 9:17

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.