On preEdit i want to select record and store it in to array, so that i can pass values postEdit

On preEdit i want to select record and store it in to array, so that i can pass values postEdit

lavanialavania Posts: 12Questions: 5Answers: 0

->on('preEdit', function ($editor, $id, $values) use ($db, &$original_data) {

})

how to select record

Link to test case:
Debugger code (debug.datatables.net):
Error messages shown:
Description of problem:

Answers

  • rf1234rf1234 Posts: 2,988Questions: 87Answers: 421
    edited October 11

    You can define a session variable for example. Save whatever you like in that variable.

    And do something with it on "postEdit" and unset it eventually.

    ->on( 'preEdit', function ( $editor, $id, $values ) {
        $_SESSION["mySavedArray"] = $values;
    } )
    ->on( 'postEdit', function ( $editor, $id, $values, $row ) {
        //do something with $_SESSION["mySavedArray"]
        //then
        unset($_SESSION["mySavedArray"]);
    } )
    

    $values is the record you are editing. If you edit multiple records you would need to loop through the array of records and save the one you want in the session variable.

    I would use your debugger and look at $values in case you are editing multiple records at the same time. If you edit a single record it is just that record in $values.

  • lavanialavania Posts: 12Questions: 5Answers: 0

    On preEdit, I want to run select query e.g.

    SELECT * from table where id=$id

    then want to pass the result to postEdit, to keep record of which variable is changed and what was the original value and what is thw new value

  • rf1234rf1234 Posts: 2,988Questions: 87Answers: 421

    If you edit multiple records you would need to loop through the array of records and save the one you want in the session variable.

    This is wrong, sorry. "preEdit" and "postEdit" are executed once for each record regardless of how many records you are editing when using multi edit.

    Ok, thanks. Now I understand your requirement a little better. I've done something like that myself, too. Let me take a look ...

    I would use "validatedEdit" and not "preEdit" because you probably don't want to save a record that eventually fails validation, I guess.

    This uses Editor's db-handler and its raw method.

    ->on( 'validatedEdit', function ( $editor, $id, $values ) use ( $db ) {
        $_SESSION['yourArray'] = [];
        
        $statement = ('SELECT * FROM table   
                        WHERE id = :id');  
        $result = $db->raw()
                     ->bind(':id', $id)
                     ->exec($statement);
        $row = $result->fetchAll(PDO::FETCH_ASSOC);
        if ( (bool)$row ) {
            $_SESSION['yourArray'] = $row;
        }
    } )
    ->on( 'postEdit', function ( $editor, $id, $values, $row ) {
        //do something with $_SESSION["yourArray"]
        //then
        unset($_SESSION["yourArray"]);
    } )
    
Sign In or Register to comment.