I've created a Symfony project using Doctrine, and when I persist an entity in the database, I see that it saves twice.
I have used both persistAndflush()
, and persist()
and flush()
separately, but I don't understand the problem.
Imagine that in your controller you have more than one operation to do on different objects: $obj1
, $obj2
, $obj3
.
Now you need to save all the transformations (create, update, delete) in your database. To tell the ORM that it needs to do these operations, you need to "fill the queue" by:
$em->persist($obj1);
$em->persist($obj2);
$em->persist($obj3);
Now in your queue you have the three objects, but still no changes in database. The flush operation tell the ORM/ODM to "now apply the changes".
$em->flush();
So the modifications applied on your three objects will be stored in your database in the order of the persist call: $obj1
, $obj2
, $obj3
.
In other words, between every 2 flush, a new transactions starts. You can compare this with the transaction, commit, rollback using normal PDO connecton
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With