重用Doctrine_Record对象以保存模型的多个实例
问题描述:
我正在研究Symfony应用程序中的某种通知模块。我遍历用户的Doctrine_Collection
为有活跃在其轮廓的标志每个用户创建一个Notification
:重用Doctrine_Record对象以保存模型的多个实例
// 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();
}
问题是,一旦我已保存的第一个通知我不能重用对象来存储新记录在数据库中。我试过$notification->setId()
与null
和''
,但保存更新对象,而不是保存一个新的。
有没有办法重用$notification
对象?我想这样做,因为通知字段创建的逻辑有点复杂。
答
Copy是你在找什么。
// 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();
}