Detect primary/unique key or constraint violation errors from PDOException
.
composer require mpyw/unique-violation-detector
Package | Version |
---|---|
PHP | ^7.1 || ^8.0 |
Database | Driver | Auto-Discoverable |
---|---|---|
MySQL | pdo_mysql |
✅ |
PostgreSQL | pdo_pgsql |
✅ |
SQLite | pdo_sqlite |
✅ |
SQLServer | pdo_sqlsrv |
✅ |
SQLServer | pdo_odbc |
|
SQLServer | pdo_dblib |
|
Oracle | pdo_oci |
✅ |
use Mpyw\UniqueViolationDetector\MySQLDetector;
// Explicitly instantiate a detector
$violated = (new MySQLDetector())->uniqueConstraintViolated($exception);
use Mpyw\UniqueViolationDetector\DetectorDiscoverer;
// Discover a detector from a PDO instance
// (Not available for pdo_odbc and pdo_dblib)
$violated = (new DetectorDiscoverer())
->discover($pdo)
->uniqueConstraintViolated($exception);