preEdit and postEdit

preEdit and postEdit

szakendreszakendre Posts: 7Questions: 2Answers: 0

Hello,
The objective is that I should get a mail at a field change.
I use these functions to check DB before and after edit.

    $editor->on( 'preEdit', function ( $editor, $id, $values )
    {
        logChange2( $editor->db(), 'pre-edit', $id, $values );
    } );
    $editor->on( 'postEdit', function ( $editor, $id, $values, $row )
    {
        logChange2( $editor->db(), 'post-edit', $id, $values );
    } );

If I check the database row at preEdit and postEdit, they show the same result: both are BEFORE edit. How can I check the final, edited values?

Or do you have a better idea to get which fields are modified with edit?

Thank you and best regards:
Endre, Szak

Replies

  • allanallan Posts: 49,303Questions: 1Answers: 7,235 Site admin

    postEdit should be the one to use. You need to make sure you are using the same database connection though (the $editor->db() result) since Editor performs its changes in a transaction by default. Can you show me your logChange2 function?

    Allan

  • szakendreszakendre Posts: 7Questions: 2Answers: 0

    Dear Allan,
    Sorry for the delay, but I was sick.

    I use my own PDO connection within logChange2 function with preEdit and postEdit too.
    The goal is that if somebody change a specified field, I have to send a warning email.
    So I save the whole row to a different table before and after editing.

    Here is my logChange2:

        $dsn = "mysql:host=$host;dbname=$db;charset=$charset";
        $opt = [
            PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
            PDO::ATTR_EMULATE_PREPARES   => false,
        ];
        $db = new PDO($dsn, $user, $pass, $opt);
    
        $oszlopok = array();
        $query = "SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA`='lakobazis2' AND `TABLE_NAME`='lakobazis'";
        $stmt = $db->query($query);
        $row_count = $stmt->rowCount();
        for ($i=0;$i<$row_count;$i++)
        {
            $row = $stmt->fetch(PDO::FETCH_ASSOC);
            $oszlopok[] = $row['COLUMN_NAME'];
        }
    
        $stmt = $db->prepare ("SELECT * FROM lakobazis WHERE torzsszam=?");
        $stmt->bindValue(1, $id, PDO::PARAM_STR);
        $stmt->execute();
        if ( $stmt->rowCount() )
        {
            $PRE = $stmt->fetch(PDO::FETCH_ASSOC);
        }
    
        $query = "INSERT INTO lakobazis_change (torzsszam";
        for ($i=1;$i<count($oszlopok);$i++)
        {
            $query .= ",".$oszlopok[$i];
        }
        $query .= ") VALUES (?";
        for ($i=1;$i<count($oszlopok);$i++)
        {
            $query .= ",?";
        }
        $query .= ")";
    
        $stmt = $db->prepare ($query);
        for ($i=0;$i<count($oszlopok);$i++)
        {
            $stmt->bindValue($i+1, $PRE[$oszlopok[$i]], PDO::PARAM_STR);
        }
        $stmt->execute();
    

    Thank You and best regards:
    Endre, Szak

  • allanallan Posts: 49,303Questions: 1Answers: 7,235 Site admin

    I use my own PDO connection within logChange2 function with preEdit and postEdit too.

    Right - that will do it because Editor's PHP libraries (by default) update the database in a transaction. That's only committed once the events have been completed.

    As a quick test for that, add ->transaction( false ) into the Editor chain (before the ->process( ... ) method is called.

    Allan

Sign In or Register to comment.