0

Assuming User is a model which extends Doctrine_Record, consider the below (arbitrary) code that creates 5000 user records.

$conn->beginTransaction();

for ($i = 1, $i < 5001, $i++) {

    $user = new User();
    $user->some_field = $i;
    $user->save();
    $user->free(true);
}

$conn->commit();

I am finding that this script is timing out after 30 seconds and not managing to create all the Users. Are there ways to optimize this code to work more quickly? I am considering just writing regular old SQL to create the records but then I lose the power of the Doctrine_Record, including insert hooks etc (Note that this script times out even if there are NO hooks in the User model).

Any advice?

Nada_Surf
  • 616
  • 1
  • 7
  • 19

1 Answers1

4

You should try the Doctrine_Collection class, which allows bulk inserts :

$collection = new Doctrine_Collection('tablename');
$collection->add($record1);
$collection->add($record2);
$collection->add($record3);
$collection->add($record4);
$collection->save();
adrien
  • 4,399
  • 26
  • 26
  • Your comment led me to another ticket that really helped: http://stackoverflow.com/questions/5415725/doctrine-insert-multiple-rows-with-just-one-save – Nada_Surf May 07 '12 at 15:28