ajkosh
(Ajaykaushiksharma087)
September 2, 2014, 12:41pm
1
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
evstevemd
(Stefano Mtangoo)
September 2, 2014, 3:49pm
2
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;
}
ajkosh
(Ajaykaushiksharma087)
September 2, 2014, 5:27pm
3
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
fwallen
(Frank Wallen)
September 3, 2014, 3:45am
4
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.
ajkosh:
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
ajkosh
(Ajaykaushiksharma087)
September 3, 2014, 4:06am
5
i need validation also and transaction also
fwallen
(Frank Wallen)
September 3, 2014, 4:33am
6
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?
evstevemd
(Stefano Mtangoo)
September 3, 2014, 1:32pm
7
You cannot use Transactions AFAIK with AR. You have to use DAO
ajkosh
(Ajaykaushiksharma087)
September 3, 2014, 2:43pm
8
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
fwallen
(Frank Wallen)
September 3, 2014, 4:02pm
9
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,
]);
}
ajkosh
(Ajaykaushiksharma087)
September 3, 2014, 6:29pm
10
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
fwallen
(Frank Wallen)
September 3, 2014, 7:05pm
11
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.
ajkosh
(Ajaykaushiksharma087)
September 4, 2014, 4:14am
12
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
fwallen
(Frank Wallen)
September 4, 2014, 5:40am
13
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.
ajkosh
(Ajaykaushiksharma087)
September 4, 2014, 6:07am
14
[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/
fwallen
(Frank Wallen)
September 4, 2014, 7:11pm
15
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
ajkosh:
ajkosh:
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.
plz 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/
evstevemd
(Stefano Mtangoo)
September 5, 2014, 8:57am
16
Wirh dao you are all alone AFAIKbut Imight be wrong. Open new thread on that may be there are some ways
pushp
(Pushpkukreja)
March 22, 2016, 10:55am
17
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.