Welcome Guest, Not a member yet? Register   Sign In
Transaction on model function
#1

(This post was last modified: 11-25-2022, 04:51 AM by pippuccio76.)

Hi , can i use transaction on model function ? 
for example :
Code:
$a=new AModel();
$b= new BModel();

$this->db->transStart();

$a->first_method;
$b->first_method();
$a->second_method();

$this->db->transComplete();

if ($this->db->transStatus() === false) {
    // generate an error

}else{
   //Success message
   //redirect
}

is it correct way ?
Reply
#2

No, you should use transactions on each model seperate.

You can look at nested transactions in the CodeIgniter 4 User Guide but I do not think that works with
multiple models.

Maybe @kenji could chime in on this.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

Transactions work on a database connection.
If the two models have the same db connection, the transaction works.
Reply
#4

In the same project is the connection on db  not always the same?
Reply
#5

Probably you use only one DB connection.

But CI can have many DB connection (database group) setting.
See https://codeigniter4.github.io/CodeIgnit...onfig-file
And can have multiple DB connections.
See https://codeigniter4.github.io/CodeIgnit...e-database
Reply
#6

Thank you @kenjis
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#7

(This post was last modified: 12-14-2022, 03:21 AM by pippuccio76.)

Hi hi have problem with transaction (same db):

Code:
            $db = \Config\Database::connect();

            $db->transStart();


            $dati_to_update_svuotamento =  [
                                            'id_materiale'=>NULL,
                                            'quantita'=>NULL,
                                            'data_carico'=>NULL,

                                          ];

            $res_svuotamento = $locazioni_piani_model->update($locazione_da_svuotare->id,$dati_to_update_svuotamento);

            $dati_to_update_riempimento =  [
                                                'quantita'=>$totale_compattato,
                                                'data_carico'=>$data_da_utilizzare,

                                          ];

            $res_riempimento = $locazioni_piani_model->update($locazione_da_riempire->id,$dati_to_update_riempimento);


            //tolgo il lock_compattazione a fine compattazione
            $data_to_update = [
                'lock_compattazione' => 0,
                'id_users'=>NULL
            ];

            $locazioni_piani_model->update([$locazione_da_svuotare->id, $locazione_da_riempire->id ], $data_to_update);

            $dati_to_insert_movimentazione= [
                                                'id_users'=>session()->get('user_id'),
                                                'id_materiale'=>$locazione_da_riempire->id_materiale,
                                                'id_locazioni_piani'=>$locazione_da_svuotare->id,
                                                'id_locazioni_piani_per_movimentazione' =>$locazione_da_riempire->id,
                                                'id_tipo_movimentazione' =>6,
                                                'quantita'=>$locazione_da_svuotare->quantita

                                            ];

            $movimenti_magazzino_model->insert($dati_to_insert_movimentazione);



            $db->transComplete();


            if ($db->transStatus() === false) {
               
                    $data['messaggi_errore'] = ' Errore , rieseguire tutto il processo ';

                    $data ['redirect'] =base_url();

                    echo view('common/messaggi_errore',$data );           
             
               
            }else{
           

                    $data['messaggi_ok'] = 'Operazione andata a buon fine';

                    $data ['redirect'] =base_url();

                    echo view('common/messaggi_ok',$data ); 
            }

I tranfer quantity from one record to another record and set to null several culumns 
Sometimes it does until :

            $res_riempimento = $locazioni_piani_model->update($locazione_da_riempire->id,$dati_to_update_riempimento);




and before don't set to null the culumn as in this case (as this image) :

https://ibb.co/LdXZybh
Reply
#8

(This post was last modified: 12-16-2022, 01:46 PM by pippuccio76.)

It's a bug? Why doesnt transaction  work properly?
Reply
#9

Can u help me ?
Reply
#10

What's the problem?
Your code is difficult to understand. And I don't understand what the screenshot means.

Can you show more simple code to reproduce your issue?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB