XF 2.2 XF\Db\DuplicateKeyException: MySQL query error [1062]: Duplicate entry

AndyB

Well-known member
How can I prevent server errors like this:

XF\Db\DuplicateKeyException: MySQL query error [1062]: Duplicate entry

I have the following code:

PHP:
if (empty($forumListCount))
{
	$forumListCount = \XF::em()->create('Andy\ForumListCount:ForumListCount');
	$forumListCount->count_date = $today;
	$forumListCount->count_guest = 0;
	$forumListCount->count_member = 0;
	$forumListCount->count_robot = 0;
	$forumListCount->save();
}

The server error only occurs once in a while. I understand this is a "race condition" and it's okay to ignore the server error message.

My question, is there any way to prevent this race condition or suppress the server error log?

Thank you.
 
I would just wrap it under a try catch block and do nothing.
PHP:
try
{
   $forumListCount->save();
}
catch (\XF\Db\DuplicateKeyException $e) {}
 
Top Bottom