ExpressionEngine

2.5.0 User Guide

ExpressionEngine Channel Entries API

Calling the Class

The Channel Entries class is called with the api->instantiate() function.

$this->EE->load->library('api');
$this->EE->api->instantiate('channel_entries');

Note: the API uses a Singleton pattern and does not currently support nesting of calls. Thus instantiating a new call while in the middle of a request may have unanticipated results.

Function Reference

Submit New Entry

This function will create a new channel entry. The data array must contain a title, an entry date, and data for all required fields.

$this->EE->api_channel_entries->submit_new_entry((int) $channel_id, (mixed) $data);
returns:(bool) Successfully Created Entry

Example Usage:

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

$data = array(
        'title'         => 'Breaking News Story!',
        'entry_date'    => '1256953732',
        'field_id_6'    => 'Some data',
        'field_ft_6'    => 'none',
        'field_id_19'   => 'More data',
        'field_ft_19'   => 'xhtml'
);

$this->EE->api_channel_fields->setup_entry_settings($channel_id, $data);

if ($this->EE->api_channel_entries->submit_new_entry($channel_id, $data) === FALSE)
{
        show_error('An Error Occurred Creating the Entry');
}

See also setup_entry_settings() in the Channel Fields API.

Note: as part of the data normalization, custom data with a value of NULL is transformed to an empty string before database insertion.

Update Entry

This function will update a channel entry. The data array must contain a title, an entry date, and data for all required fields.

$this->EE->api_channel_entries->update_entry((int) $entry_id, (mixed) $data);
returns:(bool) Successfully Updated Entry

Note: as part of the data normalization, custom data with a value of NULL is transformed to an empty string before database insertion.

Delete Entry

This function will delete one or more entries as well as some of their related data. The data array must contain an entry id, or an array of entry ids.

$this->EE->api_channel_entries->delete_entry((mixed) $entry_ids);
returns:(bool) Successfully Deleted Entry

Entry Exists

This function checks if an entry with a given id exists.

$this->EE->api_channel_entries->entry_exists((int) $entry_id);
returns:(bool) Entry Exists

Send Pings

This function sends pings to a list of ping servers. The submit_new_entry() and update_entry() functions will automatically send pings if given ping_servers in their data array. $ping_servers should be a list of ping server ids from the exp_ping_servers database table.

$this->EE->api_channel_entries->send_pings((mixed) $ping_servers, (int) $channel_id, (int) $entry_id);
returns:(bool) Pings Sent

Update Relationship Cache

This function updates the relationship cache table. You should only need to use this function if you are manually changing relationship data, submit_new_entry() and update_entry() will automatically recompile relationship data.

$this->EE->api_channel_entries->update_related_cache((int) $entry_id);

User Contributed Notes

Posted by: siffring on 1 December 2011 12:43pm
siffring's avatar

If you get unexpected PHP errors like this:

Message: Undefined index: field_type
Filename: api/Api_channel_fields.php
Line Number: 259

Try adding this:

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

after this:

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

 

Posted by: Mark Huot on 24 February 2011 3:56pm
Mark Huot's avatar

The return value for `submit_new_entry` is mixed, at best. Here’s what it’s returning as of 2.1.4 build 20110201:

* If the user isn’t authorized to post/edit entries it returns FALSE.
* If an error is encountered with the data (i.e. bad URL Title) and autosave is set to TRUE (in the third parameter) it returns an array of errors.
* If an error is encountered with the data (i.e. bad URL Title) and autosave is set to FALSE (the default) it returns FALSE.
* If no errors are encountered and autosave is set to TRUE it returns the entry_id.
* If no errors are encountered and autosave is set to FALSE (the default) it returns TRUE.

In the instance to that FALSE is returned, you can get a list of errors by accessing the API Class’ errors variable:

$this->EE->api_channel_entries->errors 
Posted by: iain on 13 January 2011 4:06pm
iain's avatar

You can get the entry_id from the Submit New Entry method using $this->EE->api_channel_entries->entry_id;

Example code:

if ($this->EE->api_channel_entries->submit_new_entry($channel_id$data) === FALSE)
{
    $error_log 
.= $i ') An Error Occurred Importing: ' $operator['nameofbusiness'"\n";
}
else
{
    $imported_ids[] 
$this->EE->api_channel_entries->entry_id;
Posted by: Nathan Pitman (Nine Four) on 13 January 2011 12:11pm
Nathan Pitman (Nine Four)'s avatar

Thanks Iain for that extra bit of info, if only I’d scrolled down to read your comment about an hour back!

It would be neat to see this added to the documentation proper and also have the Submit New Entry function return the resulting entry id rather than true or false. smile

Posted by: iain on 13 January 2011 7:33am
iain's avatar

To add category data via the api, you need to pass category ids within an array

$data = array(
    
'title'           => $operator['nameofbusiness'],
    
'entry_date'    => time(),
    
'channel_id'    => $channel_id,
    
'category'       => array($category_id),
    
'field_id_1'    => $operator['activitydetails'],
    
'field_ft_1'    => 'xhtml',
... 

You must have an ExpressionEngine license and have attained a forum rank of "Lab Assistant" (100 posts) to contribute notes to the User Guide