Write Inside Loop (ARCH7052)
هذا المحتوى غير متوفر بلغتك بعد.
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.
Incorrect
Section titled “Incorrect”<?phpforeach ($rows as $row) { $user = new User(); $user->email = $row['email']; $user->save(); // DB round-trip every iteration}Correct
Section titled “Correct”<?php$batch = array_map(fn($row) => [ 'email' => $row['email'],], $rows);
User::insert($batch); // single bulk writeCollect rows and persist them with one bulk insert/update outside the loop.