【问题标题】:Reuse a Doctrine_Record object to save multiple instances of a model重用 Doctrine_Record 对象来保存模型的多个实例
【发布时间】:2011-09-02 09:44:19
【问题描述】:

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

// 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 对象?我想这样做是因为通知字段创建的逻辑有点复杂。

【问题讨论】:

    标签: symfony1 save reusability doctrine-collection


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2021-06-07
      • 1970-01-01
      • 2017-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多