开发者

Reuse a Doctrine_Record object to save multiple instances of a model

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('...');
$notificat开发者_Python百科ion->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.


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();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜