重用 Doctrine_Record 对象来保存模型的多个实例

发布于 2024-12-02 18:22:13 字数 716 浏览 2 评论 0原文

我正在 Symfony 应用程序中开发某种通知模块。我正在迭代用户的Doctrine_Collection,为每个在其个人资料中具有活动标志的用户创建一个通知

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

问题是,一旦我保存了第一个通知,我就无法重用该对象以在数据库中存储新记录。我已尝试使用 null''$notification->setId() ,但保存会更新对象而不是保存新对象。

有没有办法重用 $notification 对象?我想这样做,因为通知字段创建的逻辑有点复杂。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人│生佛魔见 2024-12-09 18:22:13

复制是什么你正在寻找。

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

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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文