Where to do DB process before updating the row

Where to do DB process before updating the row

obrienjobrienj Posts: 93Questions: 38Answers: 0

I have a requirement to update some other rows in the current table and some other tables based on new values in the row being edited.

Right now I use the following general structure in my PHP:

<?php

// DataTables PHP library
include( "C:/wamp64/www/distLibs/phpLibs/DataTables-Editor/DataTables.php" );

// Alias Editor classes so they are easy to use
use
DataTables\Editor,
DataTables\Editor\Field,
DataTables\Editor\Format,
DataTables\Editor\Mjoin,
DataTables\Editor\Options,
DataTables\Editor\Upload,
DataTables\Editor\Validate;


// Build our Editor instance and process the data coming from _POST
Editor::inst( $db, 'calendar.events', 'RID' )
    ->fields(
            .
            .
            .
    )

    ->process( $_POST )

       ->json();

Where is the best place to do my processing before I update the edited row?

An example would be helpful.

Regards,
Jim

This question has an accepted answers - jump to answer

Answers

  • tangerinetangerine Posts: 3,342Questions: 35Answers: 394

    You probably need Editor's PHP preEdit.
    There's a little info in this thread:
    https://datatables.net//forums/discussion/38926

    Be careful here - DataTables has a js function with the same name, which is the most usual "find" when searching the forum.

  • obrienjobrienj Posts: 93Questions: 38Answers: 0

    Thanks for the input but I need to get more basic and ask the question differently.

    Referring to the PHP in the original post, within the "fields" array lets say there is a field definition:

    Editor::inst( $db, 'calendar.events', 'RID' )
        ->fields(
                  .             
    
                  Field::inst( 'allDay' ),
                  .
                  )
    
        ->process( $_POST )
    
             ->json();
    

    How would I add the following logic which is shown as pseudo-code:

    if allDay = true then
    myfunc(current_db_object, row_columns_array)

    where "myFunc" would process other rows in the current db using the data from the current row.

    To add to the complexity, I would like to do the "myFunc" processing after the current-row has been updated or added.

    Doing the "myFunc" processing before the current_row is updated or added would be OK if that is the only reasonable way to do it.

    Regards,
    Jim

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin

    Hi Jim,

    There isn't a way of doing what you are looking for in the Editor PHP libraries directly. Each row is treated individually there, rather than providing options to process them collectively.

    Instead what you would need to do is get the data returned from the database and the processing however you need:

    $data = Editor::inst( $db, 'calendar.events', 'RID' )
        ->fields(
                  .            
     
                  Field::inst( 'allDay' ),
                  .
        )
        ->process( $_POST )
        ->data();
    
    // process $data
    
    // send to client
    echo json_encode( $data );
    

    Allan

  • obrienjobrienj Posts: 93Questions: 38Answers: 0

    Allan,

    OK, I'm starting to see the approach.

    One clarification that I don't believe changes your answer that much.

    I don't want to send additional data ($data, in you example) back to the client.

    The result of processing $data are changes to the DB (adding, deleting, updating other rows).

    Does this introduce any issues to your answer?

    Now off to learn yet another programming language (PHP)..

    Regards,
    Jim

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin
    Answer ✓

    Hi Jim,

    If you want to take some specific action when Editor performs a CRUD operation, the events it triggers is the way to do it.

    Allan

This discussion has been closed.