Welcome Guest, Not a member yet? Register   Sign In
datamapper overzealous transactions
#1

[eluser]Miky llanderal[/eluser]
Hello everybody! thanks for your help in other occasions, now I have a new question and I'll call you!

I need to work with transactions and I'm using datamapper, I checked the guide and ok it's easy when you only gonna work with a single table, but I searched examples when you have to work with multiple tables and I didn't find any examples, I'm gonna put a simple example of that I want and we will see if somebody can explain me please.

I have 3 tables, 1.-users, 2.-tutors 3.- tutorials, and i need to save data in those tables,

tutorials is related with tutors and tutors with users, so when I'm going to register a new user i need to save first in users and then get the user id and register into tutors and then register the tutorials of the tutor.

The situation is that the records of the tables depend on each other so: I need to save the user in users table if the register was succesfull register the tutors then if the register on tutors table was succesfull finally record the tutorials. I thought something like this:
Code:
function registro(){
        $user=new Usuario_model();
        $user->nombre_usuario=$data1;
        $user->password=$data2;
        if($user->save()){
            $user->user_data=md5($user->id.$user->data1);
            $user->save();
        }
        if($user->trans_status()==FALSE){
            $user->trans_rollback();
             // Add error message
        $user->error_message('transaction', 'The transaction failed to save (insert)');
        }
        else{
             $tutor=new tutor_model();
             $tutor->data1="some data";
             $tutor->user_id=$u->id;
             etc etc...
             $tutor->save()
            if($tutor->trans_status()==FALSE){
               $tutor->trans_rollback();
               $user->trans_rollback();
               $tutor->error_message('transaction', 'The transaction failed to save   (insert)');
            }
            else{
                 $tutorials=new tutorials_model();
                 $tutorials->data1="some data";
                 $tutorials->tutor_id=$tutor->id;
                  ...etc
                 $tutorials->save();
                 if($tutorials->trans_status()==FALSE){
                      $tutorials->trans_rollback();
                      $tutor->trans_rollback();
                      $user->trans_rollback();
                    $tutor->error_message('transaction', 'The transaction failed to save                    (insert)');
                  }
                  else{
                   $tutorials->trans_commit();
                   $tutor->trans_commit();
                   $user->trans_commit()
                  }
            }

        }
    }
Am I right? or isn't neccesary to put the three commit calls when it was totally succesfull?
some ideas? i would thank you for your help
#2

[eluser]WanWizard[/eluser]
Assuming that these tree tables are related, you want to encapsulate them in a single transaction, so you can rollback them all if something fails. That's the general idea behind a transaction.
MySQL (and some other platforms as well) do not support nested transactions, so in the above example, as soon as you start a second transaction, the first one will be implicitly committed. And that's not what you want.

Just use a single transaction (any of the three objects will do) to encapsulate all three save() calls, and do a rollback if either of them fails. If you feel uncomfortable using one of the objects, just use CI's transaction methods ($this->db->trans_start(), $this->db->trans_complete(), etc).
#3

[eluser]Miky llanderal[/eluser]
thanks again for your help!




Theme © iAndrew 2016 - Forum software by © MyBB