mm_11111
(Marcinkowski M)
April 28, 2015, 2:50pm
1
Hi,
I have problem with transaction in loop.
I open transaction In loop and commit data only if all is correct, if no then rollback transaction and continue loop. Here all work ok.
But problem is when I try save AR model after loop. Validation model is correct save method return true but data not save in my database.
lukos
(Lukiebriner)
April 28, 2015, 4:28pm
2
There is nothing magic here. Test whether the data you commit inside the transaction has been committed correctly. If it hasn’t, you have a problem with the transaction code. If it does, then you have an error in your AR model code or the controller class somewhere so it is not saving. Perhaps it is saving as a new record and you expect an update? Perhaps the other way round?
mm_11111
(Marcinkowski M)
April 28, 2015, 6:07pm
3
Check this example, I think that its correct but I don’t know why data after my loop don’t save in database
foreach ($rows as $row) {
$transaction = Yii::$app->db->beginTransaction();
try {
...
$transaction->commit();
} catch (\Exception $e) {
$transaction->rollBack();
$log = new Log();
...
$log->save(); //correct save data in database
continue;
}
}
$TestOb = Test::findOne(1);
$TestOb->setAttribute('test', 100);
if($TestOb->validate()){
echo 'valid'; //return valid
}else{
echo 'invalid';
}
$status = $TestOb->save(); //model atrribute are correct - but no data in database
var_dump($status); //return true
Lukos:
There is nothing magic here. Test whether the data you commit inside the transaction has been committed correctly. If it hasn’t, you have a problem with the transaction code. If it does, then you have an error in your AR model code or the controller class somewhere so it is not saving. Perhaps it is saving as a new record and you expect an update? Perhaps the other way round?