diff --git a/framework/db/IntegrityException.php b/framework/db/IntegrityException.php new file mode 100644 index 0000000000..84899260e2 --- /dev/null +++ b/framework/db/IntegrityException.php @@ -0,0 +1,25 @@ + + * @since 2.0 + */ +class IntegrityException extends Exception +{ + /** + * @return string the user-friendly name of this exception + */ + public function getName() + { + return 'Integrity constraint violation'; + } +} diff --git a/tests/unit/framework/db/CommandTest.php b/tests/unit/framework/db/CommandTest.php index 03f9589984..0bb51bf33e 100644 --- a/tests/unit/framework/db/CommandTest.php +++ b/tests/unit/framework/db/CommandTest.php @@ -287,4 +287,16 @@ class CommandTest extends DatabaseTestCase public function testDropIndex() { } + + public function testIntegrityViolation() + { + $this->setExpectedException('\yii\db\IntegrityException'); + + $db = $this->getConnection(); + + $sql = 'INSERT INTO profile(id, description) VALUES (123, \'duplicate\')'; + $command = $db->createCommand($sql); + $command->execute(); + $command->execute(); + } }