Wednesday, September 14, 2016

Yii2 Transaction


Transaction: 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 = Yii::$app->db->beginTransaction();
            try  {
               if($model->save()){
                $itemprice->item_id=$model->id;
                $itemprice->price=$model->price;
                $itemprice->item_cost=$model->item_cost;
                $itemprice->wholesale=$model->wholesale;
                $itemprice->taxed=$model->tax;
                $itemprice->type=$model->taxed;
                $itemprice->save(false);
                if($model->discount==1){
                    $discount->amount=$discountamount;
                    $discount->date_from=$discountdate_from;
                    $discount->date_to=$discountdate_to;
                    $discount->item_id=$model->id;
                    $discount->type=$discounttype;
                    $discount->status=1;
                    $discount->date_from=strtotime($discount->date_from);
                    $discount->date_to=strtotime($discount->date_to);
                    $discount->save(false);
                }
                $transaction->commit();
                return $this->redirect(['view', 'id' => $model->id]);
            }
} catch (Exception $e) {
                $transaction->rollBack();
            }
OR
$transaction = $connection->beginTransaction();
try {
$user = $connection->createCommand()->insert('user', [
'name' => 'jibon',
'email' => 'jibon.bikash@gmail.com',
])->execute();
$connection->createCommand()->insert('user_role', ['role = "admin",
'user_id'=$user->id
])->execute();

$transaction->commit();

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

Yii2 Transaction


Transaction: 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 = Yii::$app->db->beginTransaction();
            try  {
               if($model->save()){
                $itemprice->item_id=$model->id;
                $itemprice->price=$model->price;
                $itemprice->item_cost=$model->item_cost;
                $itemprice->wholesale=$model->wholesale;
                $itemprice->taxed=$model->tax;
                $itemprice->type=$model->taxed;
                $itemprice->save(false);
                if($model->discount==1){
                    $discount->amount=$discountamount;
                    $discount->date_from=$discountdate_from;
                    $discount->date_to=$discountdate_to;
                    $discount->item_id=$model->id;
                    $discount->type=$discounttype;
                    $discount->status=1;
                    $discount->date_from=strtotime($discount->date_from);
                    $discount->date_to=strtotime($discount->date_to);
                    $discount->save(false);
                }
                $transaction->commit();
                return $this->redirect(['view', 'id' => $model->id]);
            }
} catch (Exception $e) {
                $transaction->rollBack();
            }
OR
$transaction = $connection->beginTransaction();
try {
$user = $connection->createCommand()->insert('user', [
'name' => 'jibon',
'email' => 'jibon.bikash@gmail.com',
])->execute();
$connection->createCommand()->insert('user_role', ['role = "admin",
'user_id'=$user->id
])->execute();

$transaction->commit();

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

Yii2 Transaction


Transaction: 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 = Yii::$app->db->beginTransaction();
            try  {
               if($model->save()){
                $itemprice->item_id=$model->id;
                $itemprice->price=$model->price;
                $itemprice->item_cost=$model->item_cost;
                $itemprice->wholesale=$model->wholesale;
                $itemprice->taxed=$model->tax;
                $itemprice->type=$model->taxed;
                $itemprice->save(false);
                if($model->discount==1){
                    $discount->amount=$discountamount;
                    $discount->date_from=$discountdate_from;
                    $discount->date_to=$discountdate_to;
                    $discount->item_id=$model->id;
                    $discount->type=$discounttype;
                    $discount->status=1;
                    $discount->date_from=strtotime($discount->date_from);
                    $discount->date_to=strtotime($discount->date_to);
                    $discount->save(false);
                }
                $transaction->commit();
                return $this->redirect(['view', 'id' => $model->id]);
            }
} catch (Exception $e) {
                $transaction->rollBack();
            }