doumei1955 2014-05-27 13:31
浏览 20
已采纳

Symfony2,重构,一个或多个flush()?

I'm trying to refactor my code in my controller.

For now, i have all treatments in this controller and i finish with a flush() in an action.

So, in an action, i can have many persist() and just one flush() at the end.

I'm working now to clean my controller, and create functions in all my repository to reduce the code.

But now i have a problem, if, before, for an action i was 10 persist() and 1 flush(), now i have 10 flush() (one for each repository-function).

I think it is not a good solution to increase the number of flush() like that, right ?

I have find something and i want to now, if with this code, there is always 10 flush() each time ? Or just one when commit() ?

$em->getConnection()->beginTransaction(); // suspend auto-commit
try {
    //... do some work
    $user = new User;
    $user->setName('George');
    $em->persist($user);
    $em->flush();
    $em->getConnection()->commit();
} catch (Exception $e) {
    $em->getConnection()->rollback();
    $em->close();
    throw $e;
}
  • 写回答

1条回答 默认 最新

  • doushao8399 2014-05-28 08:49
    关注

    In your example it's a transaction, which is good if you want to recover your flushed changes if an Exception occurs. In transactions the commit() will actually save your work, and the rollback() will revert the changes.

    In general situation you should avoid too much flush() as wll, because it's consume your resources, and there will be a lot unnecessary database action. I think the repository is mainly for finding querys and not for "flushing" querys. If you want to do every "flush query" in repository, you may want to persist the classes in your repostory and at the end of your controller you can flush the EntityManager.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大