I am working on angularjs POS Application. When i store a POS Data. I need to store, Customer Details and Item details, notes, meta and ledger entries, cash entries and all to save with relation.
The One time save may finish this all with relation. But i am afraid of its integrity. while storing, If the app encountered some kind of issue with mysql,, and some part of it is saved, then it might be an error in accounts field,
Can you provide me a better solution for this, That is if any error occurred, Data should not be saved in any table, Does your ORM Support it.?
The ORM methods save() and delete() support transactions. If your database platform supports transactions (for example MySQL with InnoDB tables), you can pass TRUE as second parameter to enable transactions with commit/rollback.
If you want to wrap multiple ORM commands into a single transaction, you have to do that manually, using the commands:
Assuming you only use one database connection (the default), something like this. Obviously, you need to add code the handle all possible errors properly, some might not need a rollback.
// start the transaction DB::start_transaction();
try { // get the users shopping cart $cart = Model_Cart::find(1);
// get the article $article = Model_Article::find(1);
// Add the item to the cart $cart->items[] = Model_Item::forge(array('article' => $article, 'amount' => 1));
// update the cart if ( ! $cart->save()) { throw new \Exception('adding item to cart failed'); }
// adjust the article stock $article->stock = $article->stock - 1; if ( ! $article->save()) { throw new \Exception('article update failed'); }
// all went well, commit the transaction DB::commit_transaction(); } catch (\Exception $e) { // an error occured, sort out the error // and rollback if needed using: DB::rollback_transaction(); }