Menus

Thursday, 3 August 2017

Yii2 Transaction


Yii2 save related records in single save call in single transaction


Database Transaction in Yii


A transaction is used to run a group of operations in single process. When you we run multiple query in single process, It will be used. If you get any problem, It will not complete successfully and also rollback the executed query.


Transaction represents a DB transaction.
It is usually created by calling yii\db\Connection::beginTransaction().
The following code is a typical example of using transactions (note that some DBMS may not support transactions):
$transaction = $connection->beginTransaction();
try {
    $connection->createCommand($sql1)->execute();
    $connection->createCommand($sql2)->execute();
    //.... other SQL executions
    $transaction->commit();
} catch (\Exception $e) {
    $transaction->rollBack();
    throw $e;
} catch (\Throwable $e) {
    $transaction->rollBack();
    throw $e;
}

Example

$transaction = Yii::$app->db->beginTransaction(); 

 try { 

 $user = new User(); 
 $user->name = 'Name'; 
 $user->save(); 

 $ua = new UserAddress(); 
 $ua->city = 'City'; 
 $user->link('userAddress', $ua); // <-- it creates new record in UserAddress table with ua.user_id = user.id 

 $transaction->commit(); 

 } catch (Exception $e) { 
 $transaction->rollBack(); 
 }


Example


if ($model->load(Yii::$app->request->post()))
        {
         
           $transaction = \Yii::$app->db->beginTransaction();
           try {  
         
         
            $stockitem=Stockitem::find()->where(['id'=>$model->stockitem_id])->one();
            $stockitem->stock=$stockitem->stock-$model->stock;
         
            $stockitem->save();
         
            //Stock add to center
            $st=  Stockcenter::find()->where(['stockitem_id'=>$model->stockitem_id,'center_ccid'=>$model->center_ccid])->one();
            if($st)
            {
              $st->stock=$st->stock+$model->stock;
              $st->amount=$model->amount;
              $st->save();
            }
            else
            {
                $st= new Stockcenter();
                $st->stockitem_id=$model->stockitem_id;
                $st->center_ccid=$model->center_ccid;
                $st->stock=$model->stock;
                $st->amount=$model->amount;
                $st->save();              
            }
         
         
            $model->date=date('Y-m-d',strtotime($model->date));
            $model->save();
         
            $transaction->commit();

            } catch (Exception $e) {
                        $transaction->rollBack();
            }
         
            //Redirect...
         
         
        }


Example

$transaction = Yii::$app->db->beginTransaction(); 
              
                try {
                    
                                     
                    $command=Yii::$app->db->createCommand('UPDATE receipt SET type="STUDENTS" , students_admission_number = '. $std->admission_number . ', students_ccid = '. $std->ccid .' WHERE type = "ENQUIRY" AND students_admission_number = '. $model->enquiry_number . ' and students_ccid = ' . $model->ccid);
                    
                    if($std->save() && $model->save() && $command->execute())                    
                        $transaction->commit();
                    
                } catch (\Exception $e) {
                    $transaction->rollBack();
                    throw $e;
                } catch (\Throwable $e) {
                    $transaction->rollBack();
                    throw $e;
                }


http://www.yiiframework.com/doc-2.0/yii-db-transaction.html


No comments:

Post a Comment