Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Integrity of Database
  • Dear,

    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.?

  • HarroHarro
    Accepted Answer
    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:

    \DB::start_transaction();
    \DB::commit_transaction();
    \DB::rollback_transaction();

    If you use multiple database instances, make sure you select the correct one.
  • can you provide a example with two orm queries...

    Very Useful Then
  • HarroHarro
    Accepted Answer
    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();
    }

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion