我插入一个条目,其中有主键的重复项。
public function actionInc()
{
$add = new Country();
$add->code = 'QI';
$add->name = 'Qiang';
$add->population = '4444444';
try {
$add->save();
return $this->render('inc', [
'count' => 'Ok',
]);
} catch (Exception $exception) {
return $this->render('inc', [
'count' => 'Error',
]);
}
}
Run Code Online (Sandbox Code Playgroud)
但我需要该应用程序不关闭,并继续工作,但不起作用...

小智 6
检查您要在使用语句
yii 中导入的Exception子类是否引发\yii\db\Exception了与数据库相关的错误。
yii的所有异常都继承自\ Exception
// db related exceptions
catch (\yii\db\Exception $exception)
// any exception throwin by yii
catch (\yii\base\Exception $exception)
// any php exception
catch (\Exception $exception)
Run Code Online (Sandbox Code Playgroud)