I want to ask about the possibility of persisting two entities when submitting a form. Is that possible?
$em = $this->getDoctrine()->getManager();
$em->persist($a);
$em->persist($b);
$em->flush();
knowing that I have commun fields between the two entities. Actually the fields are generated because of the many to one relation between the two entities. Moreover, I will get some fields added to the second entity.
Yes, it's ok to persist two entities, but check first your entities relations, for examples if A has a oneToMany
relation with B, You'll then have a addB()
method within A
entity.
Persisting related entities in this specific use case is as simple as adding cascade={"persist"}
on your relation and persist A
.
If you've some specific simple logic you want to add to B
before saving both A
with its B
related elements, you can put this logic in addB()
so that each time a B
element is added to your A
entity, you'll get the change (set the right status, date, ...etc) you want.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.