From e60bc7c3876d8c79ceccf9fc8451e28cd0b460f0 Mon Sep 17 00:00:00 2001 From: Jonathan Labreuille Date: Fri, 12 Feb 2016 19:24:04 +0100 Subject: [PATCH] handle duplicates in model --- lib/Models/Model.php | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/Models/Model.php b/lib/Models/Model.php index e3d3237377..3776bb11e4 100644 --- a/lib/Models/Model.php +++ b/lib/Models/Model.php @@ -42,7 +42,24 @@ class Model extends \Sudzy\ValidModel { } catch(\Sudzy\ValidationException $e) { $this->setError($e->getValidationErrors()); } catch(\PDOException $e) { - $this->setError($e->getMessage()); + switch($e->getCode()) { + case 23000: + preg_match("/for key \'(.*?)\'/i", $e->getMessage(), $matches); + if(isset($matches[1])) { + $column = $matches[1]; + $this->setError( + sprintf( + __('Another record already exists. Please specify a different "%1$s".'), + $column + ) + ); + } else { + $this->setError($e->getMessage()); + } + break; + default: + $this->setError($e->getMessage()); + } } catch(\Exception $e) { $this->setError($e->getMessage()); }