Integrity constraint violation not intercepted

In db table is there unique index key for two fields:

UNIQUE KEY `coord` (`lat`,`lng`),

In model gii maked this rule:

[['lat', 'lng'], 'unique', 'targetAttribute' => ['lat', 'lng'], 'message' => 'The combination of Latitudine and Longitudine has already been taken.']

But if I try to insert the same data twice, the error is not intercepted by Yii:

Integrity constraint violation – yii\db\IntegrityException

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry

How can control this, please ?

try {

    // whatever you're doing

} catch (IntegrityException $e) {

    // handle duplicate entry case


Yes, thank you. But in my case, it’s better to check for duplicate before save. But that came to my mind after posting this question, of course. :slight_smile:

Thank you again.