I want to use transactions to avoid data loss on (for example) SQL fails. This works good with:
$this->db->begin();
if($user->delete() == false) {
$this->db->rollback();
}
.. other SQL statements ..
$this->db->commit();
When one of the queries failed, the record is still in the database, as expected. But there is one problem. The model I want to delete has a relation. The related model has an afterDelete() function that delete files from the server. The files are also deleted when the transaction failed and is rolled back, so the afterDelete function is also called when the transaction failed. How can I avoid this?