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

Adding categories via the Channel Entries API

Development and Programming

pesto9's avatar
pesto9
1 posts
about 14 years ago
pesto9's avatar pesto9

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: 2085

I 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.

       
the3mus1can's avatar
the3mus1can
426 posts
about 14 years ago
the3mus1can's avatar the3mus1can

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)  ;
       }
       
pesto9's avatar
pesto9
1 posts
about 14 years ago
pesto9's avatar pesto9

Works! Thank you VERY much @the3mus1can.

       

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.