[TYPO3-dev] t3lib_tstemplate duplicate entry bug
Martin Kutschker
Martin.Kutschker at n0spam-blackbox.net
Fri Jul 13 10:02:49 CEST 2007
Jonas Dübi schrieb:
> 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.
To quote the Mysql documentation:
"For UPDATE statements, if you specify the CLIENT_FOUND_ROWS flag when
connecting to mysqld, mysql_affected_rows() returns the number of rows
matched by the WHERE clause. Otherwise, the default behavior is to return
the number of rows actually changed."
Unfortunately this flag is not supported by the PHP extension mysql. At
least not officially. I guess that the client flags of mysql_connect() are
exactly the same as those of the C API, so finding out the bit value of the
options may do the trick. The same is true for mysqli and its
mysqli_init(), mysqli_options(), mysqli_real_connect() combo.
Masi
More information about the TYPO3-dev
mailing list