Transaction In Yii2

hi i want to save model using transaction

below is my code

this is code of my controller

public function actionCreate()

{


  $model = new Company();


  $connection = \Yii::$app->db;


  $transaction = $connection->beginTransaction(); 


  try {


   if ($model->load(Yii::$app->request->post()) && $model->save()) {


        return $this->redirect(['view', 'id' => $model->name]);


    } else {


        return $this->render('create', [


            'model' => $model,


        ]);


    }


     $transaction->commit();





    } catch(Exception $e) {


       $transaction->rollback();


   }


}

it is not saving into database

plz help me

you do not insert anywhere. I can see a good mix of AR and DAO which is poisonous

Here is example from guide which you should read anyway


$transaction = $connection->beginTransaction();

try {

	$connection->createCommand($sql1)->execute();

	$connection->createCommand($sql2)->execute();

	// ... executing other SQL statements ...

	$transaction->commit();

} catch(\Exception $e) {

	$transaction->rollBack();

	throw $e;

}

but in create function form is validating and model is simple saving then how to handle validation and save form into db same as yii2 is doing ,but with transaction

if i pass sql1 and sql2 then sql injection can be problem

ajkosh, your model may be failing the validation on your post data. You should check the rules in your model and make sure they will accept your data.

i need validation also and transaction also

I was not suggesting that you do not validate, but perhaps the save() is failing because of a validation error. Have you tried outputting the message from the Exception in your catch?

You cannot use Transactions AFAIK with AR. You have to use DAO

if i m doing with dao then how to handle validation nd render the new record with id that i m saving in db

plz help me

If the $model->save() fails, you could throw an Exception instead of the render you have in the else. Move the render outside that try catch.




public function actionCreate()

{

    $model = new Company();

    $connection = \Yii::$app->db;

    $transaction = $connection->beginTransaction();

    try {

        if ($model->load(Yii::$app->request->post()) && $model->save()) {


            $transaction->commit();

            return $this->redirect(['view', 'id' => $model->name]);

        } else {

            throw Exception('Unable to save record.');

        }


    } catch(Exception $e) {

        $transaction->rollback();

    }


    return $this->render('create', [

                'model' => $model,

            ]);

}



how to check transaction is working fine nd throw exception is not working

You will have to start doing some debugging. In the catch do something like this:




catch(Exception $e) {

        $transaction->rollback();

         echo $e->getMessage(); exit;

    }



Remember, that if the transaction worked properly, then you would have a new record in your database.

i want to check with error,is it possible if any error will come it shows error on same page of create form,or send it to error page

Have you tried dumping the model or any of the data as I was recommending? You haven’t said so. If you looked at the model or the error in the try/catch, you could see that the model may contain validation errors. If you pass that on to the view with the form you can display the errors there. If you are using an activeform, it can do some of that for you.

[quote=“FrankW, post:13, topic:73164”]

Have you tried dumping the model or any of the data as I was recommending? You haven’t said so. If you looked at the model or the error in the try/catch, you could see that the model may contain validation errors. If you pass that on to the view with the form you can display the errors there. If you are using an activeform, it can do some of that for you.

[/quoplz check this if it is possible,here is my code nd my concept wat i m doing

http://www.yiiframework.com/forum/index.php/topic/57225-save-multiple-record-with-transaction/

If you use the code example I had given you and your view uses the ActiveForm widget to build your form, it can handle the errors for you. I recommend you read about ActiveForm here: https://github.com/yiisoft/yii2/blob/master/docs/guide/start-forms.md

Wirh dao you are all alone AFAIKbut Imight be wrong. Open new thread on that may be there are some ways

I know its too late to reply.I think $transaction->commit(); is after return statement. So it will never work. I am posting because, might be it can help anybody in future for the same reason.