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 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题