Ir al contenido

Write Inside Loop (ARCH7052)

Esta página aún no está disponible en tu idioma.

Key php.orm.write-inside-loop · Severity error · Category orm

Calling save()/flush per row inside a loop means one database round-trip per item. This multiplies network and transaction overhead instead of grouping writes into a single batch.

<?php
foreach ($rows as $row) {
$user = new User();
$user->email = $row['email'];
$user->save(); // DB round-trip every iteration
}
<?php
$batch = array_map(fn($row) => [
'email' => $row['email'],
], $rows);
User::insert($batch); // single bulk write

Collect rows and persist them with one bulk insert/update outside the loop.