예제 #1
0
 /**
  * Update user VCS bindings usernames (UserAccount relations).
  *
  * Returns true if each account successfully updated.
  *
  * Need to set deletionFlag to delete account from database.
  *
  * @param User $user User's model to wich need update usernames
  * @param UserAccountForm[] $accounts Usernames form models
  *
  * @return boolean true if each account successfully updated
  */
 public function updateVcsBindings(User $user, array $accounts)
 {
     $success = false;
     $transaction = $user->getDb()->beginTransaction();
     try {
         $cnt = 0;
         foreach ($accounts as $account) {
             /* @var $account UserAccountForm */
             if (!$account->isNewRecord && $account->user_id != $user->id) {
                 // this model do not belongs to current user
                 continue;
             }
             if ($account->deletionFlag) {
                 // remove account
                 $cnt += $account->delete() == 1 ? 1 : 0;
             } else {
                 $account->user_id = $user->id;
                 $cnt += $account->save() ? 1 : 0;
             }
         }
         $transaction->commit();
         $success = $cnt == count($accounts);
     } catch (Exception $ex) {
         $success = false;
         $transaction->rollBack();
     }
     return $success;
 }