Editor -> php -> postCreate -> get last insert id

Editor -> php -> postCreate -> get last insert id

cokechiucokechiu Posts: 40Questions: 14Answers: 5
edited May 2016 in Free community support

I would like to get the last insert id after create a new record, below is the code I use.

Editor::inst( $db, 'category', 'categoryId' )
        ->fields(
            Field::inst( 'categoryId' ),
            Field::inst( 'dorder' ),
            Field::inst( 'description' )
        )
        ->on('postCreate',function( $editor, $id, $values, $row ) {
            $editor->db()
                ->query('update', 'category')
                ->set('dorder',$id,false)
                ->where('categoryId', $id)
                ->exec();
        })
        ->process( $_POST )
        ->json();

but $id is nothing, any suggestions ?

Thanks.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,916Questions: 1Answers: 10,149 Site admin

    The does look like it should work!

    When you say $id is nothing, if you add var_export( $id ); into your postCreate function, what does it show in the response from the server (it will be invalid JSON, but it would be useful to know what it actually is if not the id).

    Allan

  • cokechiucokechiu Posts: 40Questions: 14Answers: 5
    edited May 2016

    Sorry for late reply, since I am busy at another app project.

    Demo link,
    http://cms.kineticspace.net/alex/dayday8/category.php

    Chrome -> network -> categoryDao.php response

    <<{"version":"1.5.5"}>>
    <<"">>
    <<{"categoryId":"","dorder":"","description":"<div>test<\/div>"}>>
    <<null>>
    {"data":[]}
    

    categoryDao.php

    ini_set("display_error","1");
        include( "datatable/Editor-PHP-1.5.5/php/DataTables.php" );
        
        // Alias Editor classes so they are easy to use
        use
            DataTables\Editor,
            DataTables\Editor\Field,
            DataTables\Editor\Format,
            DataTables\Editor\Join,
            DataTables\Editor\Upload,
            DataTables\Editor\Validate;
        
        $d = Editor::inst( $db, 'adminCategory', 'categoryId' )
            ->fields(
                Field::inst( 'categoryId' ),
                Field::inst( 'dorder' ),
                Field::inst( 'description' )
            )
            ->on('postCreate',function( $editor, $id, $values, $row ) {
                echo "<<".json_encode($editor).">>\n";
                echo "<<".json_encode($id).">>\n";
                echo "<<".json_encode($values).">>\n";
                echo "<<".json_encode($row).">>\n";
                /*
                $editor->db()
                    ->query('update', 'category')
                    ->set('dorder',$id,false)
                    ->where('categoryId', $id)
                    ->exec();
                */
            })
            ->process( $_POST )
            ->json();
    

    Any more information need to provide ?

    Thank you very much.

  • allanallan Posts: 61,916Questions: 1Answers: 10,149 Site admin

    Could you change:

    Field::inst( 'categoryId' )

    To be:

    Field::inst( 'categoryId' )->set( false )
    

    please?

    The reason for that is that Editor will automatically handle the primary key column for you.

    Could you also confirm that categoryId is an auto incrementing sequence column in your database?

    Allan

  • cokechiucokechiu Posts: 40Questions: 14Answers: 5

    Nice , work after set(false), so will it work too if I remove inst categoryid ?

  • allanallan Posts: 61,916Questions: 1Answers: 10,149 Site admin
    Answer ✓

    Yes. If you don't need to display the primary key value to your end user, remove it. The only reason to include it in the field list is if you want the end user to be able to see it (which is relatively unusual, but not uncommon!).

    Allan

This discussion has been closed.