Editor -> php -> postCreate -> get last insert id
Editor -> php -> postCreate -> get last insert id
cokechiu
Posts: 40Questions: 14Answers: 5
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
This discussion has been closed.
Answers
The does look like it should work!
When you say
$id
is nothing, if you addvar_export( $id );
into yourpostCreate
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
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
categoryDao.php
Any more information need to provide ?
Thank you very much.
Could you change:
To be:
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
Nice , work after set(false), so will it work too if I remove inst categoryid ?
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