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!
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