1

I hope that I can explain this succinctly. Platform is EE 2.10.1 (this is a sandbox, live version will be on 2.11.1).

I am working on an extension which is using Solspace's User Module's user_register_end hook. After entering the user to the member list, we need to process company information into the Companies channel.

I am achieving this by loading the channel entries and channel fields API and then calling the save_entry method:

ee()->load->library('api');
ee()->api->instantiate('channel_entries');
ee()->api->instantiate('channel_fields');

No problem so far. However, I need to process the information further and require the ID of the newly created company entry.

As far as I see, the save_entry method is only returning a success boolean - not the ID of the successfully created channel entry. First thought was to load the channel_entries_model to get the channel entry by field name. But get_entry only accepts the entry ID, which I don't have. Then I reckoned, I could use the get_recent_entries method of the channel_entries_model. However, when I run it I get the following error:

A PHP Error was encountered
Severity: Notice
Message: Undefined property: EE::$cp
Filename: core/Model.php
Line Number: 50
Fatal error: Call to a member function allowed_group() on a non-object in /var/www/html/system/expressionengine/models/channel_entries_model.php on line 189

How can I get the ID of the channel entry that has just been created through the API? I can create a custom query using Codeigniter - but would rather not.

Thanks for guidance and help.

3

Well, logically the last submitted insert query from the API is in the channel_data table. This tables Primary Key is entry_id, so you could try this CI command after you run the save_entry method:

$new_entry_id = ee()->db->insert_id();

Check to make sure it's returning the correct entry_id. I'm assuming this works within an execution cycle and uses a stored return in the database class and the insert_id method doesn't actually asynchronously retrieve that insert id via another SQL query. If it does retrieve it via a second query, you might run into collisions where two processes are spawned at near the same time and something like this happens:

process 1: save_entry
process 2: save_entry
process 1: insert_id (get's process 2's insert_id)
process 2: insert_id (get's process 2's insert_id);

Check the database class in CI core the database class to verify how the last inserted id is retrieved.

Ref: https://ellislab.com/codeigniter/user-guide/database/helpers.html

Edit

insert_id() uses this function: http://php.net/manual/en/function.mysql-insert-id.php

I think it should work fine for you. You can always do post data validation if you feel it's truly necessary, once you have your entry_id.

Edit

Try this after you save a new entry:

$new_entry_id = ee()->api_channel_entries->entry_id;
| improve this answer | |
  • I tried using insert_id. But called from the extension, the result is 0. – EXODev May 19 '16 at 15:30
  • Edited, try grabbing that prop. – jrothafer May 19 '16 at 15:34
  • @jrothafer is correct, after an entry is saved, the $entry_id class property should be set on the Api_channel_entries object. – Derek Jones May 19 '16 at 15:37
  • I will try that. Already off work. Will let you know how it goes. – EXODev May 19 '16 at 15:39
  • Using the $entry_id property on the Api_channel_entries object does the trick! Thanks. – EXODev May 23 '16 at 2:31

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.