persist () and flush() inside loop - Doctrine

I want to know how many times the ff: codes will make a roundway trip to the database.

foreach ($recipients as $recipient ){
        $received_email = new  ReceivedEmail();
        $received_email->setRecipient($recipient);
        $received_email->setEmail($email);
        $entityManager->persist($received_email);
        $entityManager->flush(); 
        }

$recipients is an array of User objects with One-to-Many relationship with ReceivedEmail

$email is an object with One-to-Many relationship with ReceivedEmail.

If for instance $recipients has five entities, does the loop generates a total of five trips to database? Or only one?

Is the above example the most optimized way to insert new ReceivedEmail records?

Thanks

Answers


There will be generated one INSERT statement for each persist.

What is more, you can just display yours SQL statements for debug, just configure Doctrine for logging: http://stackoverflow.com/a/4585421/1815881

In your case:

$entityManager->getConfiguration()->
                setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger());

For many inserts you should consider the batch prcessing:

Hope I have revealed the problem.


Need Your Help

Query to aggregate contract line-item values across contracts

sql ms-access group-by

I have 2 tables in Microsoft Access: Contract_1 & Contract Items

Intel intrinsics : multiply interleaved 8bit values

c intel sse simd intrinsics

I'm working on a RGBA32 buffer (8bits per component), and I'd need to multiply each component by a constant, then add each of the results of the multiplication to the others as such :

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.