我想知道 ff: 代码将往返数据库多少次。
foreach ($recipients as $recipient) {
$received_email = new ReceivedEmail();
$received_email->setRecipient($recipient);
$received_email->setEmail($email);
$entityManager->persist($received_email);
$entityManager->flush();
}
$recipients
是与 ReceivedEmail 具有一对多关系的 User 对象数组
$email
是与 ReceivedEmail 具有一对多关系的对象。
例如,如果$recipients
有五个实体,那么循环是否会生成总共五次到数据库的行程?还是只有一个?
上面的示例是插入新 ReceivedEmail 记录的最优化方式吗?
谢谢