Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between persistAndFlush and persist, flush

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.

like image 467
Bilel Avatar asked May 20 '15 08:05

Bilel


2 Answers

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.

like image 198
Alouini Khalil Avatar answered Oct 13 '22 08:10

Alouini Khalil


In other words, between every 2 flush, a new transactions starts. You can compare this with the transaction, commit, rollback using normal PDO connecton

like image 36
Mohan Avatar answered Oct 13 '22 08:10

Mohan