I need to be able to add category information using the Channel Entries API. I saw that there is an example for this at the bottom of the CE API documentation page. But I can’t seem to get this to work, it will insert a new entry in exp_channel_data & exp_channel_titles but not add the entry to the exp_category_posts table.
Each entry will have four categories that it needs to be tied to: a parent category, a child category, a style category, and a brand category.
Can someone give a better example of how to do this or tell me what I am doing wrong? Here’s my code:
$channel_id = '1';
$category_id = array("10","13","31","35");
$data = array(
'title' => $STGrow->field_id_28,
'entry_date' => $this->EE->localize->now,
'channel_id' => $channel_id,
'category' => array($category_id),
'field_id_1' => $STGrow->field_id_28,
'field_id_5' => $STGrow->field_id_29,
'field_id_20' => $STGrow->field_id_30,
'field_id_7' => $STGrow->field_id_31,
'field_id_9' => $STGrow->field_id_32,
'field_id_10' => $STGrow->field_id_33,
'field_id_11' => $STGrow->field_id_34
);
$this->EE->api_channel_fields->setup_entry_settings($channel_id, $data);
if ($this->EE->api_channel_entries->submit_new_entry($channel_id, $data) === FALSE)
{
print_r ($this->EE->api_channel_entries->errors) ;
}When I run the above I get this error:
A PHP Error was encountered
Severity: Notice
Message: Array to string conversion
Filename: mysql/mysql_driver.php
Line Number: 551
Error Number: 1054
Unknown column 'Array' in 'field list'
INSERT INTO `exp_category_posts` (`entry_id`, `cat_id`) VALUES (54, Array)
Filename: libraries/api/Api_channel_entries.php
Line Number: 2085I have tried to change ‘category’ => array($category_id) to ‘category[]’ => array($category_id) which makes the error go away but it doesn’t write to exp_category_posts.
Try this:
$channel_id = '1';
$categories = array("10","13","31","35");
$data = array(
'title' => $STGrow->field_id_28,
'entry_date' => $this->EE->localize->now,
'channel_id' => $channel_id,
'category' => $categories,
'field_id_1' => $STGrow->field_id_28,
'field_id_5' => $STGrow->field_id_29,
'field_id_20' => $STGrow->field_id_30,
'field_id_7' => $STGrow->field_id_31,
'field_id_9' => $STGrow->field_id_32,
'field_id_10' => $STGrow->field_id_33,
'field_id_11' => $STGrow->field_id_34
);
$this->EE->api_channel_fields->setup_entry_settings($channel_id, $data);
if ($this->EE->api_channel_entries->submit_new_entry($channel_id, $data) === FALSE)
{
print_r ($this->EE->api_channel_entries->errors) ;
}Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.