Mysql事务是否可以删除php文件?
在Yii1上,假设要保存$model1和$model2对象,则这是DB事务Mysql事务是否可以删除php文件?,php,mysql,transactions,try-catch,unlink,Php,Mysql,Transactions,Try Catch,Unlink,在Yii1上,假设要保存$model1和$model2对象,则这是DB事务 $transaction = Yii::app()->db->beginTransaction(); try{ if(!$model1->save()) throw new CDbException("Error: fail to save model1"); // successfuly save if(!$model2->save()) throw new CDbExcept
$transaction = Yii::app()->db->beginTransaction();
try{
if(!$model1->save()) throw new CDbException("Error: fail to save model1"); // successfuly save
if(!$model2->save()) throw new CDbException("Error: fail to save model2"); // If this one throws an error it will go to catch then roll back the previous one w/c the $model1
$transaction->commit();
}catch(Exception $ex){
$transaction->rollback();
}
我的问题是在PHP中删除文件是否可以适应SQL事务的思想
try{
unlink("file1.txt"); //this is successfully deleted
unlink("file2.txt"); //failed to delete this one how can I rollback the file1?
}catch(Exception $ex) {
//need to rollback the file1 or any file has been deleted or modify on try{}
}
这是可能的吗?是的,但它不像MYSQL那样简单,因为MYSQL为您完成所有繁重的工作,而对于文件删除,您必须手动执行。是否存在一个类或函数来执行此操作?或者我真的需要从头开始做。我不知道有哪一个,但谷歌可能是你在这种情况下的朋友。我已经在寻找一些想法或类/函数来做这件事,但运气不好,我有一个想法,但如果我这样做,有很多代码/逻辑需要编写。我在这里问这个问题的原因是,也许这里已经有人做过或者知道这个课程。@RiggsFolly非常感谢你的评论。