简体   繁体   中英

ORM/Doctrine2 - When to persist?

This has been bugging me for a while. In Doctrine2, we have the: ObjectManager function:

void persist(object $object = null)

You only need to call it on new entities.

My question though, is "when" should it be called? Immediately after creating the entity, or immediately before flushing it?

I can't find any documentation indicating the convention. The reason this is important is because Doctrine dispatches the "persist event" when calling.

Given that the object might still be empty, it seems to imply that any functionality tagged on to that event should disregard the importance of the data the object contains at that point in time.

Am I correct in that statement or is there a convention Doctrine promotes?

What you want to do is create your new object, use it anyway you want, and when you're done with it and want to send it to your database, then persist it, just before flushing it.

If you persisted your entity just after creating it, any changes you would make wouldn't be taken into account when sent to the database.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM