Logging preEdit values on postEdit with multi row updates

Logging preEdit values on postEdit with multi row updates

Taylor74Taylor74 Posts: 7Questions: 2Answers: 0

Hi,

I am trying to capture a specific row value before it is updated for logging purposes. This works fine with single row updates, however if a multi row update is done the preEdit value gets set and inserted as just one of the row values that was updated.

For example, lets say there is a table with two rows and column1 values of "foo" and "bar" respectively. If I do a multi update on those two rows setting the column1 value to "newValue1" for both of them, the previous column value will save as "bar" for them both (when one of them should have been "foo").

Here is the code I am using:

   ->on( 'preEdit', function ( $editor, $id, $values ) {
      global $prevValues;

      $prevValues = $editor->db()->select( $editor->table()[0], 'item_id', [ 'expense_id' => $id ] )->fetch();
  } )

  ->on( 'postEdit', function ( $editor, $id, $values, $row) {
      global $prevValues;
      
      $values = array_shift($values);
      logChange( $editor->db(), 'EDIT', $id, $values, $prevValues );
   } )

I was only able to find this thread that looks to be related:
https://datatables.net/forums/discussion/51227/preedit-and-multiple-editing

Based on Allan's response I attempted something like this, but with no luck as the $prevValues evaluates as NULL inside postEdit:

   ->on( 'preEdit', function ( $editor, $id, $values ) {
      global $prevValues;

      $prevValues = $editor->db()->select( $editor->table()[0], 'subproj_id', [ 'expense_id' => $id ] )->fetch();
  } )

  ->on( 'postEdit', function ( $editor, $id, $values, $row) use ($prevValues) {
      $values = array_shift($values);
      logChange( $editor->db(), 'EDIT', $id, $values, $prevValues );
   } )

Thanks for any assistance,
Taylor

Answers

  • Taylor74Taylor74 Posts: 7Questions: 2Answers: 0

    As always, the solution is simple and obvious. I was able to get it working by adding the $id as an index to $prevValues variable.

    Here is the working code:

       ->on( 'preEdit', function ( $editor, $id, $values ) {
          global $prevValues;
    
          $prevValues[$id] = $editor->db()->select( $editor->table()[0], 'item_id', [ 'expense_id' => $id ] )->fetch();
      } )
      ->on( 'postEdit', function ( $editor, $id, $values, $row) {
          global $prevValues;
    
          $values = array_shift($values);
          logChange( $editor->db(), 'edit expense subproject assignment', $id, $values, $prevValues[$id] );
       } )
    
This discussion has been closed.