[TYPO3-english] t3lib sql_insert_id()

Dmitry Dulepov dmitry at typo3.org
Wed Mar 18 19:52:35 CET 2009


Hi!

Marcel Remmy wrote:
> So I suppose it's: "$data[ tablename ][ uid ][ fieldname ]" & "$tce =
> t3lib_div::makeInstance('t3lib_TCEmain')" you're talking of.
> 
> So is it correct to use this function to manipulate system tables and
> then to go with
> 
> * $GLOBALS['TYPO3_DB']->sql_insert_id();
> 
> to gain knowledge of the uid of the newly created item?

Almost :) You use $tce->process_datamap() and then $tce->substNEWwithIDs. This is an array where keys are 'NEW...' values you supply in the $data and values are new uids for records. Notice that ids must start with 'NEW'!

So you can do this:
===========================
$id = uniqid('NEW');
$data = array(
	'tx_myext_table' => array(
		$id => array(
			'myfield' => $fieldValue
		) 
	)
);
$tce = t3lib_div::makeInstance('t3lib_TCEmain');
$tce->start($data, null);
$tce->process_datamap();
if (count($tce->errorLog) == 0) {
	$newRecordId = $tce->substNEWwithIDs[$id];
}
===========================

You also need to set some special fields, main of them is 'pid'.

If you are ready to spend a couple of € on my book, you can learn this and a lot more about extension development :) http://www.packtpub.com/typo3-extension-development/book

-- 
Dmitry Dulepov
TYPO3 core team
http://dmitry-dulepov.com/article/typo3-extension-update-google-sitemap.html


More information about the TYPO3-english mailing list