samedi 14 juillet 2018

Lavavel/Eloquent 5 transactions not rolling back on model

I do seem to be having problems getting database transactions to work on a model. I've referred to related posts on SO, but no luck yet.

In my example, a model with a 'name' property has a 'unique' constraint on it. So when I save the second object with the same name, it should throw.

        try{
            DB::beginTransaction();

            $oNewMap = $oMap->replicate();
            $oNewMap->name = "[test] " . $oNewMap->name;
            $oNewMap->save();  // works

            $oNewMap = $oMap->replicate();
            $oNewMap->name = "[test] " . $oNewMap->name;
            $oNewMap->save(); // throws

            DB::commit();
            /* Transaction successful. */
        }
        catch(\Exception $e){       
            DB::rollback();
            /* Transaction failed. */
        }

When the rollback occured, why wouldn't the first saved record disappear from the DB? Am I missing something with how models work with transactions?

The physical tables are all InnoDB, btw.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire