I'm using OctoberCMS and my rules is situated on model class. When occurs rules validation error, they throw new ModelException
trying:
DB::beginTransaction();
try
{
$model = new Model;
$model->name = $name;
$model->save();
$another = new Another;
$another->id = $model->id;
$another->value = $value;
$another->save();
// all right
DB::commit();
}
catch( ModelException $e )
{
// some rules exception with one of these models
DB::rollback();
}
Above code save the $model
and if one ModelException
is throw on saving $another
, the $model
record remains on database.
mysql: 5.6.21
tables: InnoDB
My workaround to make it work is:
$model = new Model;
$model->name = $name;
$model->save();
try
{
$another = new Another;
$another->id = $model->id;
$another->value = $value;
$another->save();
}
catch( ModelException $e )
{
$model->delete();
$myerrors = $another->errors()->all();
}
How make Laravel transactions work in described situation?