When using an Object Relational Mapper, how do I create new entities inside another entity and how can I persist them? I am using Doctrine 2 (PHP) but I assume this applies equally well to Hibernate (Java) and NHibernate (C#) as well.
For example, O have an Order
entity that has a setCompleted()
method. My business logic dictates that whenever an order is completed, a new Product
entity is created. Note that Order
and Product
are currently not related (should they be?). To me, the most logical place to put that business logic is inside the setCompleted()
method. But how do I tell the ORM that there's a new entity to persist? The entity manager isn't available inside the entity.
Or am I approaching this problem in the wrong way and should I implement it some other way?