Passing mysql ID of newly created row to another table

Passing mysql ID of newly created row to another table

milapmilap Posts: 40Questions: 13Answers: 2
edited January 2016 in Editor

Hello,
I am new happy (so far ;)) user of DataTables Editor.

Ive got a problem with passing mysql ID of a newly created row to another table.
I just cant get the iD value it self.

Here is my php code:

include( "../php/DataTables.php" );
use
    DataTables\Editor,
    DataTables\Editor\Field,
    DataTables\Editor\Format,
    DataTables\Editor\Join,
    DataTables\Editor\Upload,
    DataTables\Editor\Validate;

$editor = Editor::inst( $db, 'karta' )
    ->fields(
        Field::inst( 'karta.numer' )->validator( 'Validate::notEmpty' ),
                Field::inst( 'karta.sn' )->validator( 'Validate::notEmpty' ),
        Field::inst( 'karta.nazwa' )->validator( 'Validate::notEmpty' ),
        Field::inst( 'karta.czas' ),
        Field::inst( 'karta.id_s_karta' )
                ->options( 's_karta', 'id', 'nazwa' ),
                Field::inst( 's_karta.nazwa' )
        )              
    ->leftJoin( 's_karta', 's_karta.id', '=', 'karta.id_s_karta' );
$editor->process( $_POST );
$editor->data();

//print_r ($editor);
if ( isset( $_POST['action'] ) && $_POST['action'] === 'create' ) {
    $db->sql("INSERT INTO status (id_karta,id_s_status,id_uzytkownik,opis,ip)"
            . "VALUES (".$editor['data'][0]['DT_RowId'].",1,1,'xxx','1.1.1.1')");
}
$editor->json();:

Iam looking for same effect as from php mysql_insert_id() function.
I've fround very similar topic that nearly explains how to do that: https://datatables.net/forums/discussion/19217/how-to-get-create-success-in-php
But I cant get it work... - mostly because I dont know how to properly debug json response (Iam new with AJAX)
The problem is of course that the array element like $editor['data'][0]['DT_RowId'] does not exists (Ive seen that making print_r ($editor))

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    Hi,

    I'd suggest using the events that the Editor PHP libraries emit. Specifically in this case use postCreate where the id for the created row is passed into the event handler as the second parameter.

    Allan

  • milapmilap Posts: 40Questions: 13Answers: 2
    edited January 2016

    Allan,

    Following Your suggestion I have modified my code to:

    include( "../php/DataTables.php" );
    use
        DataTables\Editor,
        DataTables\Editor\Field,
        DataTables\Editor\Format,
        DataTables\Editor\Join,
        DataTables\Editor\Upload,
        DataTables\Editor\Validate;
    
    $editor = Editor::inst( $db, 'karta' )
        ->fields(
            Field::inst( 'karta.numer' )->validator( 'Validate::notEmpty' ),
                    Field::inst( 'karta.sn' )->validator( 'Validate::notEmpty' ),
            Field::inst( 'karta.nazwa' )->validator( 'Validate::notEmpty' ),
            Field::inst( 'karta.czas' ),
            Field::inst( 'karta.id_s_karta' )
                    ->options( 's_karta', 'id', 'nazwa' ),
                    Field::inst( 's_karta.nazwa' )
            )              
        ->leftJoin( 's_karta', 's_karta.id', '=', 'karta.id_s_karta' )
            ->on( 'postCreate', function ( $editor, $id, $values, $row ) {
                $editor->db()->sql("INSERT INTO status (id_karta,id_s_status,id_uzytkownik,opis,ip)"
                . "VALUES (".$id.",1,1,'XXX','1')");
            } );
    $editor->process( $_POST );
    $editor->json();
    

    ... and it works like a charm :) Thank You!

This discussion has been closed.