Created
April 5, 2013 20:18
-
-
Save hoffigk/5322299 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function createFoo() | |
{ | |
$pdo = new PDO(/*...*/); | |
$maxTryCount = 3; | |
$tryCount = 0; | |
create_foo_start: | |
try { | |
$key = createMyOwnUniqueKey(); // e.g. foo-ear62-re4it | |
$stmt = $pdo->prepare('INSERT INTO foo (key) VALUES (:key)'); | |
$stmt->execute(array(':key' => $key)); | |
return new Foo(/*...*/); | |
} catch (PDOException $e) { | |
if (preg_match('Duplicate entry.*for key \'key\'', $e->getMessage())) { | |
if ($tryCount++ < $maxTryCount) { | |
goto create_foo_start; | |
} | |
throw MyKeyIsNotReallyUniqueException('oh ein eichhörnchen. ich muss weg ...'); | |
} | |
throw new FooCreationException; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment