[TYPO3-dev] t3lib_tstemplate duplicate entry bug

Jonas Dübi admin at commandline.ch
Fri Jul 13 09:38:55 CEST 2007


Hello Everyone

For our development servers we enabled 'sqlDebug' and I often get a 
duplicate entry error for the cache_pagesection insert from 
t3lib_tstemplate script.

This coresponds with: http://bugs.typo3.org/view.php?id=5282

I found the peace of code which is responsible for this:

Starting at line: 358
> 			if (!$this->simulationHiddenOrTime && !$GLOBALS['TSFE']->no_cache)	{	// Only save currentPageData, if we're not simulating by hidden/starttime/endtime
> 				$dbFields = array(
> 					'content' => serialize($cc),
> 					'tstamp' => $GLOBALS['EXEC_TIME']
> 				);
> 				$mpvar_hash = t3lib_div::md5int($GLOBALS['TSFE']->MP);
> 				$GLOBALS['TYPO3_DB']->exec_UPDATEquery('cache_pagesection', 'page_id=' . intval($GLOBALS['TSFE']->id) . ' AND mpvar_hash=' . $mpvar_hash, $dbFields);
> 				if ($GLOBALS['TYPO3_DB']->sql_affected_rows() == 0) {
> 					$dbFields['page_id'] = intval($GLOBALS['TSFE']->id);
> 					$dbFields['mpvar_hash'] = $mpvar_hash;
> 					$GLOBALS['TYPO3_DB']->exec_INSERTquery('cache_pagesection', $dbFields);
> 				}
> 			}

It seams that the sql_affected_rows() returns 0 if the new values are 
the same as the old values in the database. Because of that the record 
gets inserted as a new record which doesn't work because that ends up in 
a duplicate entry -> the page_id and the mpvar_hash are unique.

My Suggestion would be like this:

Starting at line: 358
> 			if (!$this->simulationHiddenOrTime && !$GLOBALS['TSFE']->no_cache)	{	// Only save currentPageData, if we're not simulating by hidden/starttime/endtime
> 				$dbFields = array(
> 					'content' => serialize($cc),
> 					'tstamp' => $GLOBALS['EXEC_TIME']
> 				);
> 				$mpvar_hash = t3lib_div::md5int($GLOBALS['TSFE']->MP);
> 				$existingCacheRecords = $GLOBALS['TYPO3_DB']->exec_SELECTquery('mpvar_hash, page_id', 'cache_pagesection', 'page_id=' . intval($GLOBALS['TSFE']->id) . ' AND mpvar_hash=' . $mpvar_hash);
> 				if($GLOBALS['TYPO3_DB']->sql_num_rows($existingCacheRecords) > 0){
> 					$GLOBALS['TYPO3_DB']->exec_UPDATEquery('cache_pagesection', 'page_id=' . intval($GLOBALS['TSFE']->id) . ' AND mpvar_hash=' . $mpvar_hash, $dbFields);
> 				} else {
> 					$dbFields['page_id'] = intval($GLOBALS['TSFE']->id);
> 					$dbFields['mpvar_hash'] = $mpvar_hash;
> 					$GLOBALS['TYPO3_DB']->exec_INSERTquery('cache_pagesection', $dbFields);
> 				}
> 			}

I already tested that solution on several installations and it works great.

Regards Jonas




More information about the TYPO3-dev mailing list