0

I am working on some kind of notifications module in a Symfony application. I am iterating over a Doctrine_Collection of users to create a Notification for every user that has a flag active in its profile:

// Create and define common values of notification
$notification = new Notification();
$notification->setField1('...');
$notification->setField2('...');
...

// Post notification to users
foreach ( sfGuardUserTable::getInstance()->findByNotifyNewOrder(true) as $user ) {
  $notification->setUserId($user->getId());
  $notification->save();
}

Problem is that once I have saved the first notification I cannot reuse the object to store new records in the database. I have tried $notification->setId() with null and '', but saving updates the object instead of saving a new.

Is there a way to reuse the $notification object? I would like to do that because logic of notification fields creation is a bit complex.

j0k
  • 22,600
  • 28
  • 79
  • 90
elitalon
  • 9,191
  • 10
  • 50
  • 86

1 Answers1

1

Copy is what you're looking for.

// Create and define common values of notification
$notification = new Notification();
$notification->setField1('...');
$notification->setField2('...');
...

// Post notification to users
foreach ( sfGuardUserTable::getInstance()->findByNotifyNewOrder(true) as $user ) {
  $newNotification = $notification->copy();
  $newNotification->setUserId($user->getId());
  $newNotification->save();
}
Jakub Zalas
  • 35,761
  • 9
  • 93
  • 125