douwa6220 2011-12-18 07:18
浏览 36
已采纳

如何在Symfony2中正确保留模型?

I have been very puzzled about the concept of model (entity) persistence in Symfony2. AFAIK, there is nothing like model_instance->save() in Symfony2. Instead, it must be invoked in a controller by retrieving the doctrine entity manager, and then persist the model_instance and flush the model_instance.

  • If I have a Blog model, which contains variable Tag model instances and Category model instances, which will be generated from the blog message body. However, I do not know how I can create a Blog factory to also save these tags and categories, instead I must return these "Tag" and "Category" instances from a factory (which it won't persist anyway) --- there is no way I can persist the blog and persist the tags and categories all within a single method in the Blog.

In fact, if I want to write a script that is invoked periodically to persist models, it seems impossible, because it must invoke the controller, and yet I don't see why a script requires a web server to be present, but not just work on the object model without touching the web server.

I really appreciate how to resolve the problems above. Thank you!

  • 写回答

2条回答 默认 最新

  • drexlz0623 2011-12-18 20:55
    关注

    You're right, Doctrine2 is an implementation of the DataMapper pattern. So the concepts are different from the ActiveRecord that is implemented by some other ORMs like Doctrine1 and Propel.

    By default, when you create an entity with some relations, you have to persist your main entities and its relations manually. But you can change this behavior activating the persistence cascade:

    http://www.doctrine-project.org/docs/orm/2.1/en/reference/working-with-associations.html#transitive-persistence-cascade-operations

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作