-
-
Save DavidBruchmann/598fc0ebe92efd6f18a7cf6bfbaf7e96 to your computer and use it in GitHub Desktop.
Get MySQL error code with Doctrine DBAL
This file contains hidden or 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 | |
protected function executeQueryBuilder($queryBuilder, string $debugHeader) | |
{ | |
$resource = null; | |
try { | |
$resource = $queryBuilder->execute(); | |
} catch (\Doctrine\DBAL\DBALException $e) { | |
$previous = $e->getPrevious(); | |
if ($previous instanceof \Doctrine\DBAL\Driver\Mysqli\MysqliException) { | |
// $errorCode contains MySQL error code (ex: 1062 for a duplicate entry) | |
$errorCode = $previous->getCode(); | |
$errorMsg = $previous->getMessage(); | |
# DebuggerUtility::var_dump(['$queryBuilder' => $queryBuilder, '$errorCode' => $errorCode, '$errorMsg' => $errorMsg], $debugHeader); | |
var_dump([$debugHeader, '$errorCode' => $errorCode, '$errorMsg' => $errorMsg]); | |
} | |
} | |
return $resource; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment