IntegrityConstraintViolationException example


  public function handleExecutionException(\Exception $exception, StatementInterface $statement, array $arguments = [], array $options = []): void {
    if ($exception instanceof \PDOException) {
      // Wrap the exception in another exception, because PHP does not allow       // overriding Exception::getMessage(). Its message is the extra database       // debug information.       $message = $exception->getMessage() . ": " . $statement->getQueryString() . "; " . print_r($arguments, TRUE);
      // Match all SQLSTATE 23xxx errors.       if (substr($exception->getCode(), -6, -3) == '23') {
        throw new IntegrityConstraintViolationException($message$exception->getCode()$exception);
      }
      throw new DatabaseExceptionWrapper($message, 0, $exception);
    }

    throw $exception;
  }

}


      $message = $exception->getMessage() . ": " . $statement->getQueryString() . "; " . print_r($arguments, TRUE);

      // SQLSTATE 23xxx errors indicate an integrity constraint violation. Also,       // in case of attempted INSERT of a record with an undefined column and no       // default value indicated in schema, MySql returns a 1364 error code.       if (
        substr($exception->getCode(), -6, -3) == '23' ||
        ($exception->errorInfo[1] ?? NULL) === 1364
      ) {
        throw new IntegrityConstraintViolationException($message$code$exception);
      }

      throw new DatabaseExceptionWrapper($message, 0, $exception);
    }

    throw $exception;
  }

}
Home | Imprint | This part of the site doesn't use cookies.