laravel-save vs saveorfail(简而言之就是真正的区别) [英] laravel - save vs saveorfail (real difference in nutshell)

查看:296
本文介绍了laravel-save vs saveorfail(简而言之就是真正的区别)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

终于是我理解这个概念的时候了,因为我仍然没有得到一些案例.

It's finally time that I understood this concept, because I still don't get some of the cases.

问题1) save()返回什么?它总是布尔值还是有时会抛出异常?

Question 1) what does save() return? is it always boolean or does it throw exceptions sometimes?

问题2):我没有使用任何事件模型.因此,我认为 save()不会在任何时候返回false.那么它将返回true还是抛出异常?我说的对吗?

Question 2) I am not using any event model. So I don't think save() will return false at any time. So it will return true or throw the exception? am I right?

问题3):如果我有这样的事情:

Question 3) If I have something like this:

DB::beginTransaction();
try{
  $model1 = new Type();
  $model1->test = 'great';
  $model1->save();

  $model2 = new Type();
  $model2->test2 = 'awesome';
  $model2->save();
  DB::commit();
}catch(Exception $e){
  DB::rollBack();
}

是否有可能没有发生保存但没有引发异常?这些模型中没有任何事件.

Is it possible that save doesn't happen but it doesn't throw the exception? I don't have any events in these models.

问题4),如果问题3的答案是不可能",那么为什么我需要使用 saveOrFail()

Question 4) if the answer for the question 3 is 'no it's not possible', then why do i need to use saveOrFail()

我真的很感激,因为我真的找不到能深刻解释我所问问题的东西.

I'd really appreciate that since I really couldn't find anything that deeply explains what I am asking.

推荐答案

问题1) save()确实可以引发异常.例如,如果您创建带有小数列的模型,例如'cost',并尝试在该列中保存一个字符串值, save() saveOrFail()都将引发异常.演示:

Question 1) save() can indeed throw exceptions. For example, if you create a model with a decimal column, e.g. 'cost', and try to save a string value in that column, both save() and saveOrFail() will throw an exception. Demo:

>>> $item->cost = 'asdas';
>>> $item->save();
 Illuminate/Database/QueryException with message 'SQLSTATE[HY000]: General error: 1366 Incorrect decimal value: 'asdas' for column 'cost' at row 1 (SQL: update ...

问题2):浏览

Question 2) Looking through the source, it looks like save() will only return false when firing either the saving, updating or creating events returns false.

由于您尚未定义任何事件侦听器,因此从理论上讲,是的,您应该只接收 true ,否则将引发异常.

Since you haven't defined any event listeners, theoretically yes, you should only ever receive true or an exception will be thrown.

问题3):如果您不收听事件,则不会,这是不可能的(至少是不可能的).仅当抛出异常时,保存才会发生.

Question 3) If you're not listening to events then no, it's not possible (at least not probable). The save would only not have happened if there was an exception thrown.

问题4)由于 saveOrFail()只是将 save()包装在事务中,因此它的用途是使数据库保持一致(如果有)在 save()函数期间引发了异常. saveOrFail()确保在 save()期间出现任何异常,该模型将不会保存.如果抛出异常,单独的 save()不能保证未修改/保存模型.

Question 4) Since saveOrFail() just wraps save() in a transaction, its use is to keep the database consistent if any exceptions were raised during the save() function. saveOrFail() ensures that if there were any exceptions raised during save(), the model would not have been saved. save() alone would not be able to guarantee that the model was not modified/saved if an exception was thrown.

由于您已经将代码包装在事务中,因此无需使用 saveOrFail .

Since you're already wrapping your code in a transaction, you don't need to use saveOrFail.

对于您的用例,我能想到的最好的方法是在单个if表达式中对所有模型调用 save(),并且仅在表达式为 true .这样,当 save()返回 false 或引发异常时,您都可以满足.像这样:

For your use case, the best I can think of is to call save() on all of your models in a single if-expression and to commit the transaction only if the expression is true. That way you can cater for both when save() returns false or when it raises an exception. Like this:

DB::beginTransaction();
try {
  $model1 = new Type();
  $model1->test = 'great';
  
  $model2 = new Type();
  $model2->test2 = 'awesome';
  
  if ($model1->save() && $model2->save()) {
    DB::commit();
  } else {
    DB::rollBack();
  }
} catch(Exception $e){
  DB::rollBack();
}

这篇关于laravel-save vs saveorfail(简而言之就是真正的区别)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆