[SOLVED] How to know what ID of the insertObject was

This forum is for general questions about extensions for Joomla! version 1.5.x.

Moderator: General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Locked
rand486
Joomla! Ace
Joomla! Ace
Posts: 1888
Joined: Thu Aug 20, 2009 7:58 pm
Location: Ontario, Canada

[SOLVED] How to know what ID of the insertObject was

Post by rand486 » Wed May 26, 2010 6:33 am

I'm afraid this may be a dumb question, but I'll ask anyway.

I noticed that the call $db->insertObject() returns a void. Is there an easy way to know what the id (primary key) it created was?

Example: I have a function that creates an article in jos_content, and I want to know what the ID was of the article I just created.

I'm afraid if I query the most recently created article on a very busy server that it will pull a different article by chance on occasion. Or by sheer chance, two articles are created at the same date and time (down to the second). Hey, accidents happen.
Last edited by rand486 on Wed May 26, 2010 8:20 pm, edited 1 time in total.

User avatar
mokelly
Joomla! Guru
Joomla! Guru
Posts: 617
Joined: Sun Jul 20, 2008 10:57 am
Location: Royse City, Texas USA
Contact:

Re: How to know what ID of the insertObject was

Post by mokelly » Wed May 26, 2010 2:42 pm

Here is a way to do it. http://help.joomla.org/content/view/716/60/ Sometimes the mysql_insert_id() will work but it is always best to use the Joomla framework.
CARTwebERP Where you sell, fullfill, and account for it all!

rand486
Joomla! Ace
Joomla! Ace
Posts: 1888
Joined: Thu Aug 20, 2009 7:58 pm
Location: Ontario, Canada

Re: How to know what ID of the insertObject was

Post by rand486 » Wed May 26, 2010 8:20 pm

Awesome, thanks. I'll mark this solved.

Just to be 100% sure though, this means it will get the record I JUST entered, right?

To explain, there's no way the following could happen?

1) this php script (Instance A) is run, enters the record.
2) Another user runs the same script (Instance B), entering another record
3) Instance A now hits insertID() but returns the ID of the record created by Instance B.

The (pseudo)code would be like the following:

Code: Select all

$database->insertObject( '#__users', $user, 'id' );

//Is there a possibility a second instance of this script will run and insertObject() another record here?

return $database->insertID();

User avatar
mokelly
Joomla! Guru
Joomla! Guru
Posts: 617
Joined: Sun Jul 20, 2008 10:57 am
Location: Royse City, Texas USA
Contact:

Re: [SOLVED] How to know what ID of the insertObject was

Post by mokelly » Wed May 26, 2010 9:28 pm

I believe as long as you use the Joomla framework you are safe. I think Joomla uses the session variables to identify the user and keep track of these types of variables by user.
CARTwebERP Where you sell, fullfill, and account for it all!

rand486
Joomla! Ace
Joomla! Ace
Posts: 1888
Joined: Thu Aug 20, 2009 7:58 pm
Location: Ontario, Canada

Re: [SOLVED] How to know what ID of the insertObject was

Post by rand486 » Wed May 26, 2010 10:23 pm

Oh that's right, I totally forgot. My paranoia of threading was pointless. Thanks again!

tim92109
Joomla! Intern
Joomla! Intern
Posts: 54
Joined: Fri Mar 25, 2011 11:41 pm

Re: [SOLVED] How to know what ID of the insertObject was

Post by tim92109 » Mon Jul 24, 2017 11:38 pm

Typical Joomla forum. This has been solved? Why? Because it has a link to a page that Joomla removed? TYPICAL.


Locked

Return to “Extensions for Joomla! 1.5”