Multiple joins on the same table

Multiple joins on the same table

lucianolunalucianoluna Posts: 11Questions: 3Answers: 0
edited February 2022 in DataTables

Hello, maybe found a serious bug.

I have multiple left join on a users table because I need to store and show the used id who inserted the record and the user id who last modified the record.

    Field::inst( 'u1.username' ),
    Field::inst( 'u2.username' ),
    Field::inst( 'pratiche.ID_UtenteInserimento' )
        ->set( Field::SET_CREATE )
        ->setValue( $userlogin['details']['id'] ),
    Field::inst( 'pratiche.ID_UtenteUltimaModifica' )
        ->set( Field::SET_EDIT )
        ->setValue( $userlogin['details']['id'] )

->leftJoin( 'users as u1',      'u1.id',   '=', 'pratiche.ID_UtenteInserimento' )
->leftJoin( 'users as u2',      'u2.id',   '=', 'pratiche.ID_UtenteUltimaModifica' );

The problem is that Datatables updates the users table erroneously.
Here you have the log of the queries from the MySql database, in particular here you can see the problem:
2022-02-22T14:27:16.580442Z 74 Query SELECT * FROM users u1 WHERE id = '2'
2022-02-22T14:27:16.581046Z 74 Query UPDATE users u1 SET username = 'operatore1' WHERE id = '2'
2022-02-22T14:27:16.581961Z 74 Query SELECT * FROM users u2 WHERE id = '2'
2022-02-22T14:27:16.582541Z 74 Query UPDATE users u2 SET username = 'backoffice1' WHERE id = '2'

The column username is updated with value 'operatore1' and after with 'backoffice1' !!!!!

this is the full log:

2022-02-22T14:27:16.568970Z 74 Query START TRANSACTION
2022-02-22T14:27:16.575364Z 74 Query SELECT pratiche.id as 'pratiche.id' FROM pratiche WHERE pratiche.id = '5'
2022-02-22T14:27:16.576925Z 74 Query UPDATE pratiche SET ID_StatoPratica = '2', DataPratica = '2022-02-21', nome = 'Maria', ID_Gestore = '2', ID_Servizio = '4', ID_Sesso = '1', cognome = 'Antonelli', CodiceFiscale = 'ENNIOMORR1111112', DataNascita = '2005-02-11', LuogoNascita = 'Roma', Telefono = '0815255563', Email = '', IndirizzoResidenza = '', ComuneResidenza = '', CapResidenza = '44111', ProvResidenza = '', IndirizzoSpedizione = '', ComuneSpedizione = '', CapSpedizione = '', ProvSpedizione = '', AnnotazioneOperatore = '', AnnotazioneSupervisore = '', ID_Allegato1 = '4', ImportoServizio = '19.99', SerialeSim = '51515151516515615169999', TrackingCode = '111111112222222223333333333', VerificaEffettuata = '0', DataUltimaModifica = '2022-02-22 15:27:16', ID_UtenteUltimaModifica = '2' WHERE pratiche.id = '5'
2022-02-22T14:27:16.580442Z 74 Query SELECT * FROM users u1 WHERE id = '2'
2022-02-22T14:27:16.581046Z 74 Query UPDATE users u1 SET username = 'operatore1' WHERE id = '2'
2022-02-22T14:27:16.581961Z 74 Query SELECT * FROM users u2 WHERE id = '2'
2022-02-22T14:27:16.582541Z 74 Query UPDATE users u2 SET username = 'backoffice1' WHERE id = '2'
2022-02-22T14:27:16.583798Z 74 Query SELECT pratiche.id as 'pratiche.id', pratiche.ID as 'pratiche.ID', pratiche.ID_StatoPratica as 'pratiche.ID_StatoPratica', pratiche.DataPratica as 'pratiche.DataPratica', stati.stato as 'stati.stato', pratiche.nome as 'pratiche.nome', pratiche.ID_Gestore as 'pratiche.ID_Gestore', gestori.gestore as 'gestori.gestore', pratiche.ID_Servizio as 'pratiche.ID_Servizio', pratiche.ID_Sesso as 'pratiche.ID_Sesso', servizi.servizio as 'servizi.servizio', pratiche.cognome as 'pratiche.cognome', pratiche.CodiceFiscale as 'pratiche.CodiceFiscale', pratiche.DataNascita as 'pratiche.DataNascita', pratiche.LuogoNascita as 'pratiche.LuogoNascita', pratiche.Telefono as 'pratiche.Telefono', pratiche.Email as 'pratiche.Email', pratiche.IndirizzoResidenza as 'pratiche.IndirizzoResidenza', pratiche.ComuneResidenza as 'pratiche.ComuneResidenza', pratiche.CapResidenza as 'pratiche.CapResidenza', pratiche.ProvResidenza as 'pratiche.ProvResidenza', pratiche.IndirizzoSpedizione as 'pratiche.IndirizzoSpedizione', pratiche.ComuneSpedizione as 'pratiche.ComuneSpedizione', pratiche.CapSpedizione as 'pratiche.CapSpedizione', pratiche.ProvSpedizione as 'pratiche.ProvSpedizione', pratiche.AnnotazioneOperatore as 'pratiche.AnnotazioneOperatore', pratiche.AnnotazioneSupervisore as 'pratiche.AnnotazioneSupervisore', pratiche.ID_Allegato1 as 'pratiche.ID_Allegato1', pratiche.ImportoServizio as 'pratiche.ImportoServizio', pratiche.SerialeSim as 'pratiche.SerialeSim', pratiche.TrackingCode as 'pratiche.TrackingCode', pratiche.VerificaEffettuata as 'pratiche.VerificaEffettuata', u1.username as 'u1.username', u2.username as 'u2.username', pratiche.DataInserimento as 'pratiche.DataInserimento', pratiche.DataUltimaModifica as 'pratiche.DataUltimaModifica', pratiche.ID_UtenteInserimento as 'pratiche.ID_UtenteInserimento', pratiche.ID_UtenteUltimaModifica as 'pratiche.ID_UtenteUltimaModifica' FROM pratiche LEFT JOIN stati ON stati.id = pratiche.ID_StatoPratica LEFT JOIN gestori ON gestori.id = pratiche.ID_Gestore LEFT JOIN servizi ON servizi.id = pratiche.ID_Servizio LEFT JOIN users u1 ON u1.id = pratiche.ID_UtenteInserimento LEFT JOIN users u2 ON u2.id = pratiche.ID_UtenteUltimaModifica WHERE pratiche.id >= '0' AND pratiche.ID_UtenteInserimento = '2' AND pratiche.id = '5'
2022-02-22T14:27:16.585217Z 74 Query SELECT id as 'id', filename as 'filename', filesize as 'filesize', web_path as 'web_path', system_path as 'system_path' FROM files WHERE id IN ('4')
2022-02-22T14:27:16.585427Z 74 Query COMMIT

This question has accepted answers - jump to:

Answers

  • rf1234rf1234 Posts: 2,801Questions: 85Answers: 406
    Answer ✓

    Is it right that you don't want the username fields to be updated? I can't see what you are doing at the front end but either way you can make sure those fields don't get updated using ->set( false ).

    Field::inst( 'u1.username' )->set( false ),
        Field::inst( 'u2.username' )->set( false ),
        Field::inst( 'pratiche.ID_UtenteInserimento' )
            ->set( Field::SET_CREATE )
            ->setValue( $userlogin['details']['id'] ),
        Field::inst( 'pratiche.ID_UtenteUltimaModifica' )
            ->set( Field::SET_EDIT )
            ->setValue( $userlogin['details']['id'] )
     
    ->leftJoin( 'users as u1',      'u1.id',   '=', 'pratiche.ID_UtenteInserimento' )
    ->leftJoin( 'users as u2',      'u2.id',   '=', 'pratiche.ID_UtenteUltimaModifica' );
    
  • lucianolunalucianoluna Posts: 11Questions: 3Answers: 0

    Yes! That do the trick!

    Thank you so much!

  • rf1234rf1234 Posts: 2,801Questions: 85Answers: 406
    Answer ✓

    È stato un piacere. Buona giornata.

Sign In or Register to comment.