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

How to create Entry using php

Development and Programming

MaxStoun's avatar
MaxStoun
21 posts
15 years ago
MaxStoun's avatar MaxStoun

Good day. How to create Entry using PHP ? Are there any good manual to use PHP in EE 2.x ?

       
Focus Lab Dev Team's avatar
Focus Lab Dev Team
1,129 posts
15 years ago
Focus Lab Dev Team's avatar Focus Lab Dev Team

Hi Max. Welcome to the EE forums 😊

You can check out the EE Dev Docs for info on using some of their built-in classes and whatnot. To add entries via PHP I’d suggest you take a look at the Channel Entries API section.

       
MaxStoun's avatar
MaxStoun
21 posts
15 years ago
MaxStoun's avatar MaxStoun

Thanks!

       
MaxStoun's avatar
MaxStoun
21 posts
15 years ago
MaxStoun's avatar MaxStoun

I created template Some/test with allow php. I set code in template as

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

$data = array( 'title' => 'New entry',
        'custom_field1' => 'test1',
        'entry_date' => '1293673800',
        'alternative_header'=>'new entry'  );
 if ($this->EE->api_channel_entries->submit_new_entry(1, $data) === TRUE) {echo ('All Ok'); } 
?>

When I’m in browser load url site.com/some/test I have message “All Ok”, but entry isn’t created. What’s my mistake?

       
Focus Lab Dev Team's avatar
Focus Lab Dev Team
1,129 posts
15 years ago
Focus Lab Dev Team's avatar Focus Lab Dev Team

I’m not sure. Must be a false positive. I haven’t used that API before so I’m not sure where the issue came up.

What happens if you disable extensions? Does it work properly then?

       
MaxStoun's avatar
MaxStoun
21 posts
15 years ago
MaxStoun's avatar MaxStoun

If i disable extensions - it isn’t work properly. I see database - there are new record in table ‘exp_channel_data’, but all field in record is NULL(except channel_id, entry_id. it’s correct channel_id in database record) (if I create record manually using Content/Publish - there are some field not NULL in record)

       
Focus Lab Dev Team's avatar
Focus Lab Dev Team
1,129 posts
15 years ago
Focus Lab Dev Team's avatar Focus Lab Dev Team

Hmm, it sounds like you need to use the database column rather than the custom field short name. So rather than ‘custom_field1’ you would need to use the equivalent ‘field_id_xx’

Do you know what I mean?

       
MaxStoun's avatar
MaxStoun
21 posts
15 years ago
MaxStoun's avatar MaxStoun

Yes, it’s work. Thanks.

       
Focus Lab Dev Team's avatar
Focus Lab Dev Team
1,129 posts
15 years ago
Focus Lab Dev Team's avatar Focus Lab Dev Team

Great! 😊

       
MaxStoun's avatar
MaxStoun
21 posts
15 years ago
MaxStoun's avatar MaxStoun

How do I get entry_id from created entry ? For example -

$this->EE->api_channel_entries->submit_new_entry(1, $data);
$submittedEntryId = ?????? ;
       
MaxStoun's avatar
MaxStoun
21 posts
15 years ago
MaxStoun's avatar MaxStoun

I try to use “select from DB”, like as

select max(entry_id) from exp_channel_data where channel_id = 12 limit 1

thanks

       
MaxStoun's avatar
MaxStoun
21 posts
15 years ago
MaxStoun's avatar MaxStoun

How to create entry with different then English language in field ? For example:

$data = array('title' => 'Title',
              'entry_date' => time()
             );
$this->EE->api_channel_entries->submit_new_entry(1, $data)

return TRUE

// title => text in Russian
$data = array('title' => 'Тайтл',
              'entry_date' => time()
             );
$this->EE->api_channel_entries->submit_new_entry(1, $data)

return FALSE

EE using utf-8 as default charset and utf-8 as DB charset (if I try add entry manually adding record to DB - all ok)

Upd1: It’s problem with _validate_url_title in Api_channel_entries.php and return error string “unable_to_create_url_title” Maybe, it’s some problem with DB charset Upd2: I not implement url_title If I implement url_title all ok. Problem solved. Thanks

       
Pascal Kriete's avatar
Pascal Kriete
2,589 posts
15 years ago
Pascal Kriete's avatar Pascal Kriete

Awesome troubleshooting in this thread 😊 .

How do I get entry_id from created entry ?

Your solution will work most of the time, but there’s no guarantee that entry ids will be sequential. I’m a little surprised that it doesn’t return the id. I’ll look into that, but for now try this right after the insert:

$this->EE->api_channel_entries->entry_id;
       
Focus Lab Dev Team's avatar
Focus Lab Dev Team
1,129 posts
15 years ago
Focus Lab Dev Team's avatar Focus Lab Dev Team

Looks like I stopped getting notifications on this thread. I must have looked at something on my phone and forgotten to reply back at my desk at some point.

Sorry for the absence MaxStoun. Looks like Pascal pointed you in the direction you needed 😊

       

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.