Answer the question
In order to leave comments, you need to log in
Yii2 trigger error when trying to delete record?
I have 2 related tables in MySQL created via migrations: 'food' and 'import_log'. The latter has a trigger that prevents the associated entry from being removed from 'food':
// add foreign key for table `food`
$this->addForeignKey(
'fk-import_log-food_id',
'import_log',
'food_id',
'food',
'id',
'RESTRICT'
);
Answer the question
In order to leave comments, you need to log in
As padlyuck suggested , Yii2 doesn't handle deletion triggers on its own, so I came up with this version of the deletion action:
public function actionDelete($id)
{
try {
$this->findModel($id)->delete();
Yii::$app->session->setFlash('success', Yii::t('app', 'controller.Food.deleted'));
} catch (\Exception $e) {
Yii::$app->session->setFlash('error', Yii::t('app', 'controller.Food.cannot_delete'));
}
return $this->redirect(['index']);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question