How to get additional data on the server-side postEdit event?

How to get additional data on the server-side postEdit event?

bjshortybjshorty Posts: 20Questions: 6Answers: 0

I'm using the logging example code on the server-side events page to create an activity log for my table.

This is the code:

->on( 'postEdit', function ( $editor, $id, $values, $row ) {
    $editor->db()->insert( 'log', array(
        'user'   => $_SESSION['username'],
        'action' => 'Edit users table row',
        'values' => json_encode( $values ),
        'row'    => $id,
        'when'   => date('c')
    );
} )

What I want is to get the column label on the table, not the column name on the database just because they are different. This is the section of the code:
'values' => json_encode( $values ),

Also, there are several fields from the edited record that i'd like to add, besides the row ID:
'row' => $id,

Like: 'row2' => $Name or 'row3' => $employee_id

Any advice or suggestions are welcome!

Thank you

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,516Questions: 1Answers: 10,473 Site admin

    Hi,

    What I want is to get the column label on the table, not the column name on the database just because they are different.

    If the names of the fields in the $values array is not what you want you would need to translate them either through a database lookup or perhaps a static map. You could potentially use the Field->name() and / or the Field->db() method if you have used the ability to define a different HTTP name from the database name for a field to do that translation.

    Also, there are several fields from the edited record that i'd like to add, besides the row ID:

    I'm not quite clear why you can't just add the code you suggested to the array? You'd need access to those variables of course and also row2 etc would need to a column in the database.

    Allan

  • bjshortybjshorty Posts: 20Questions: 6Answers: 0

    I tried to, but it seems I'm doing it wrong.

    <b>Warning</b>: Missing argument 5 for {closure}() in <b>/home1/hdoadm/public_html/credenciales/examples/php/employees.php</b> on line <b>63</b><br />
    <br />
    <b>Notice</b>: Undefined variable: employee_id in <b>/home1/hdoadm/public_html/credenciales/examples/php/employees.php</b> on line <b>69</b><br />
    {"error":"SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'row2' cannot be null","data":[]}

     ->on( 'postEdit', function ( $editor, $id, $values, $row, $employee_id) {
        $editor->db()->insert( 'log', array(
            'user'   => $_SESSION['login_userName'],
            'action' => 'Edit users table row',
            'values' => json_encode( $values ),
            'row'    => $id,
            'row2'   => $employee_id,
            'when'   => date('c')
        )
        );
        }
        )
    
  • allanallan Posts: 63,516Questions: 1Answers: 10,473 Site admin
    Answer ✓

    You can't add an extra argument to the function call - Editor is the one that defines what arguments it passes in.

    Without being able to see the full code, I can't say for sure, but can't you do something like:

    'row2' => $row['employee_id']
    

    Depending entirely on where the employee id is and what it is called of course. Is it in the row object?

    Allan

  • bjshortybjshorty Posts: 20Questions: 6Answers: 0

    Awesome. It worked perfectly! Thank you.

This discussion has been closed.