Is there a callback for the $db->update() method?

Is there a callback for the $db->update() method?

TomBajzekTomBajzek Posts: 163Questions: 36Answers: 1

I have an application that uses the $db->update() method to update a field in a related table. Next, I need to read back that related table (using a PDO query, as there is no editor for that table in this file), to build a groups fie for HTTP AUTH. I find that when I read back the related table I've updated, the update has not yet happened in the related table. It looks like I need a callback for the $db->update() method. I could not find documentation indicating that such a callback exists. Am I missing something, or tell me how I can get a callback with the $db->update() has finished?

Thanks,
Tom

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 61,972Questions: 1Answers: 10,160 Site admin

    The method is synchronous, so no callback is needed. You just need to execute your code after it.

    Without being able to see the hole code it is hard to say what is going wrong, but I’d guess that Editor is in a transaction, and thus you need to use $editor->db() to get the database connection in that transaction, or disable the transaction using ->transaction(false) on the Editor instance.

    Allan

  • TomBajzekTomBajzek Posts: 163Questions: 36Answers: 1

    Allan,

    The issue arises in this piece of code:

    function updateUserRecords($db, $newEmail, $active) {
        // Update the related email fields, and the adminGroups.active field
        global $htpasswd;
        global $theEmail;
        global $db;
        global $username;
        global $password;
        $db->update('adminGroups',
            ['email'=>$newEmail],
            ['email'=>$theEmail]
        );
        $db->update('adminGroups',
            ['active'=>$active],        // Update adminGroups.active
            ['email'=>$theEmail]
        );
        $db->update('docViewGroups',
            ['email'=>$newEmail],
            ['email'=>$theEmail]
        );
        $db->update('preferences',
            ['email'=>$newEmail],
            ['email'=>$theEmail]
        );
    
        // Now read in the adminGroups table to build the groups file
        $aG = new PDO( 'mysql:host=localhost;dbname=userMgmt', $username, $password ) or die("Can't connect to DB!");
        $groupData = $aG->query('select * from adminGroups where active = "yes" order by email')->fetchAll(PDO::FETCH_ASSOC) or die("Can't retrieve adminGroup data!");
        $truth = true;
        buildAdminGroupsFile( $groupData );
    }
    

    The updateUserRecords function is called from a postEdit event on another table, roles. This file has no editor for the adminGroups table, just for the roles table.

    The problem is that the data in the adminGroups table is not available when the PDO query of the adminGroups table is executed at line 27. Examining the data via a breakpoint on line 28 confirms that the adminGroups table has not yet been updated at that point, although it does get updated if you disable the breakpoint and let the entire process finish.

    Seeing you answer let me to think my question needed some clarification...

    Thanks,
    Tom

  • allanallan Posts: 61,972Questions: 1Answers: 10,160 Site admin
    Answer ✓

    Hi Tom,

    Did you try adding ->transaction(false) to your Editor class instance (immediately before the ->process(...) method call is the typical place to do it? Certainly, it sounds a lot like the issues is due to the transaction Editor uses.

    Allan

  • TomBajzekTomBajzek Posts: 163Questions: 36Answers: 1

    Hi Allan,

    Adding ->transaction(false) fixed the problem. I didn't understand your prior answer correctly, and thought I need to elaborate on the problem. I should have just tried the ->transaction(false). I'm sorry I bothered you unnecessarily.

    Thanks again,
    Tom

  • allanallan Posts: 61,972Questions: 1Answers: 10,160 Site admin
    Answer ✓

    No worries - good to hear that helped. Does it make sense now?

    Allan

This discussion has been closed.