It is usually created by calling [[Connection::beginTransaction()]]. The following code is a typical example of using transactions (note that some DBMS may not support transactions): php $transaction = $connection->beginTransaction(); try { $connection->createCommand($sql1)->execute(); $connection->createCommand($sql2)->execute(); .... other SQL executions $transaction->commit(); } catch (\Exception $e) { $transaction->rollBack(); throw $e; }
Since: 2.0
Author: Qiang Xue (qiang.xue@gmail.com)
Inheritance: extends yii\base\Object
 protected function endTransaction($result)
 {
     if ($this->transaction) {
         if ($result) {
             $this->transaction->commit();
         } else {
             $this->transaction->rollBack();
         }
     }
 }
 public function afterTransaction()
 {
     if ($this->hasErrors()) {
         if ($this->ownerAddErrors) {
             foreach ($this->relations as $relation) {
                 if ($this->errors[$relation]) {
                     $this->owner->addErrors($this->errors[$relation]);
                 }
             }
         } else {
             $this->owner->addError('ALL', 'error');
             // что бы работал owner->hasErrors
         }
     }
     if ($this->transaction !== null) {
         if ($this->hasErrors()) {
             $this->transaction->rollBack();
         } else {
             $this->transaction->commit();
         }
     }
 }
Exemple #3
-1
 /**
  * @param \netis\crud\db\ActiveRecord $model
  * @param null|yii\db\Transaction $transaction
  */
 protected function afterExecute($model, $transaction)
 {
     if ($model->getBehavior('trackable') !== null) {
         $model->endChangeset();
     }
     if ($transaction !== null) {
         $transaction->commit();
     }
 }
 protected static function withdrawSuccessful(Invoice $invoice, Transaction $transaction)
 {
     $invoice->status = 'success';
     $invoice->user->account -= abs($invoice->amount);
     if ($invoice->user->save() && $invoice->save()) {
         $transaction->commit();
         Yii::$app->session->setFlash('success', Yii::t('app', 'Payment #{id} completed', ['id' => $invoice->id]));
     } else {
         Yii::$app->session->setFlash('error', json_encode(array_merge($invoice->user->errors, $invoice->errors), JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
     }
 }
 /**
  * Rollsback a DB transaction if valid
  *
  * @param null|Transaction $transaction
  */
 protected static function tranRollback($transaction)
 {
     if ($transaction && $transaction instanceof Transaction) {
         $transaction->rollback();
     }
 }
Exemple #6
-1
 /**
  * @inheritdoc
  */
 protected final function runInternal()
 {
     if ($this->beforeCrudActionRun()) {
         if ($this->multiple && !empty($this->model)) {
             $this->transaction = $this->model[0]->getDb()->beginTransaction();
         }
         try {
             if (isset($this->modelAction)) {
                 Yii::info('Model method to be executed: ' . $this->modelAction, __METHOD__);
                 $this->runCrudAction($this->model);
             }
             if ($this->multiple && !empty($this->model)) {
                 $this->transaction->commit();
             }
             $this->result = true;
         } catch (\Exception $e) {
             if ($this->multiple && !empty($this->model)) {
                 $this->transaction->rollBack();
             }
             $this->result = false;
             Yii::error(VarDumper::dumpAsString($e), __METHOD__);
         }
         $this->afterCrudActionRun();
     }
 }
Exemple #7
-1
 public function open(Transaction $transaction)
 {
     try {
         $sum = (int) Node::find()->where(['user_name' => $this->user_name])->count();
         $sum += (int) Income::find()->where(['user_name' => $this->user_name])->count();
         if ($this->invest()) {
             do {
                 $i = $this->rise();
             } while ($i > 0);
             if (0 == $sum && $this->user->canChargeBonus()) {
                 $referral = $this->user->referral;
                 $referral->account += $this->type->bonus;
                 $referral->update(true, ['account']);
                 if (4 == $this->type->id) {
                     $count = $referral->getSponsors()->select('user_name')->joinWith('nodes')->groupBy('user_name')->count();
                     if ($count > 0 && 0 == $count % 10) {
                         $gift = new Gift(['user_name' => $referral->name]);
                         $gift->save();
                         Yii::$app->session->setFlash('success', Yii::t('app', 'Your referral may receive a gift'));
                     }
                 }
             }
             $transaction->commit();
             Yii::$app->session->setFlash('success', Yii::t('app', 'The plan is open'));
             return true;
         } else {
             Yii::$app->session->setFlash('error', $this->dump());
         }
     } catch (\Exception $ex) {
         $transaction->rollBack();
         Yii::$app->session->setFlash('error', $ex->getMessage());
     }
     return false;
 }
Exemple #8
-1
 public function commit(Transaction $transaction)
 {
     $transaction->commit();
 }