dongxibo2095 2016-06-28 19:17
浏览 56

如何使用带有Doctrine的Symfony 3将对象持久保存在控制器之外的数据库中

I am trying to separate data access from my controllers in a Symfony 3 application. Symfony allows Doctrine mediated DB queries to be made via a repository class that extends the EntityRepository class, but I'm having trouble finding a way to persist objects to the DB outside of a class that inherits from the controller base class. Simply put: How can I persist an Entity object to a DB via Doctrine outside of a controller in Symfony?

Thanks for your help.

  • 写回答

1条回答 默认 最新

  • dongqi6964 2016-06-28 21:04
    关注

    You have two options ,

    Say that the class you want to persist objects with a class named DBManager

    1. Define DBManager as a service and inject doctrine entity manager into this class. This thread may help you
    2. Make DBManger extends ContainerAware, like that you will already have entity manager along with other services like mailer, logger, etc. Take a look at this answer for more details on how to do it
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号