Answer the question
In order to leave comments, you need to log in
How to catch a database error (Exception) in Yii2 Active Record?
Hello. Yii2 + Postgesql.
An ID field of type UUID.
I'm trying
try {
$file = Files::find()->where(['CREATED_BY' => \Yii::$app->user->id, 'ID' => $id])->one();
} catch (Exception $ex) {
throw new \yii\web\NotFoundHttpException('Запись не найдена');
}
Answer the question
In order to leave comments, you need to log in
Which Exception class are you using (give namespace)? Try using \yii\db\Exception or global \Exception in general.
Because find() does not return an error, but returns null, so in this case, you need to check for null or not
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question