M
M
McMike2017-07-12 13:33:32
PostgreSQL
McMike, 2017-07-12 13:33:32

How to catch an error when deleting a model?

There is an entry in the table that they are trying to delete. It could be referenced from other tables, respectively, in this case, you need to display a message about the impossibility of deletion.
How to check if a post can't be deleted?
try {
$model->delete();
} catch (Exception $e) {
}
Such code catches this exception from a DB, the script falls down with an error.
How to catch an exception, or is there something like the canDelete() method in Yii2 to check if the deletion is possible?
I do not want to manually check if there are links to this record in other tables.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Bay, 2017-08-15
@kawabanga

Suddenly needed.
www.yiiframework.com/doc-2.0/yii-db-integrityexcep...

D
Dmitry, 2017-07-12
@slo_nik

Good afternoon.
Do you mean post id?
Use a foreign key.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question