On PreEdit question

On PreEdit question

nessinitsnessinits Posts: 86Questions: 27Answers: 0

Hi,

This is a PHP library question. I want to create a mechanism that makes notifications of the modified data. Is there a simpel solution to determine the difference between the database data and the posted data before it written to the database? Do I need to query the database or is the data already available?

Kind regards,
nessinits

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 63,457Questions: 1Answers: 10,465 Site admin
    Answer ✓

    The easiest option would be to use the changed option of the form-options object. That way the client-side will only submit the data that has actually changed value.

    If you want to do something more complex like a proper diff, then yes, you'd need to read the old data from the database first.

    Allan

  • nessinitsnessinits Posts: 86Questions: 27Answers: 0

    Thanks Allan,

    It's the more complex thing I want to do. It's not the answer I hoped for, but it's an answer I can work with.

    Best regards,
    nessinits

  • rf1234rf1234 Posts: 2,984Questions: 87Answers: 421
    Answer ✓

    @nessinits
    Here is some sample code using Editor's database handler which is available here. Took me a while to figure out how to use it. Based on this you could select the record and diff it with what is coming from the client:

    ->on('preEdit', function ( $editor, $id, $values ) {  
        //if the front end does not send the operator we need to read it
        if ($values['report_has_filtr']['operator'] <= '') {
            $result = $editor->db()->raw()
            ->bind( ':id', $id )
            ->exec( 'SELECT operator  
                       FROM report_has_filtr  
                      WHERE id = :id' );
            $row = $result->fetch(PDO::FETCH_ASSOC);
            $values['report_has_filtr']['operator'] = $row["operator"];
            $editor->field('report_has_filtr.operator')->set( false );
        }            
    })
    

    fetch all and get the row count:

    $row = $result->fetchAll(PDO::FETCH_ASSOC);
    $rowCount = $result->count();
    

    another one with DELETE

    ->on( 'postRemove', function ( $editor, $id, $values ) {
        logChange( $editor->db(), 'delete', $id, $values, 'gov_manual_creditor' );
    //delete contracts that have the manual creditor id as a foreign key.
        $editor->db()->raw()
           ->bind( ':fk', $id )
           ->exec( 'DELETE FROM contract   
                     WHERE gov_manual_creditor_id = :fk' );
    } )            
    
  • allanallan Posts: 63,457Questions: 1Answers: 10,465 Site admin

    I guess one other option would be to use initEdit to copy the values of the form when editing on a row is triggered into hidden "dummy" fields. Then when the form is submitted you would have both the data as it was before editing and the data after editing. Any diff could then be done between them. Didn't think of that last night - sorry!

    Allan

This discussion has been closed.