Get the row id of the newly created row
Get the row id of the newly created row
I'm calling PHP server-side to use the databable editor. When clicking on New I can create my new record. I also want to create a reference to this new record in another MySQL table. I can do this by calling another method.
All good so far. I just cannot work out how to capture the id of the row just created.
Any advice appreciated. I'll post my code below. You can see I have two vars I need to get. The item_id is the row of the new ly created record, but I don't know how to access this yet. So for the example I have it set to 123...
// Build our Editor instance and process the data coming from _POST
Editor::inst( $db, 'mtp_event_schedule_items', 'item_id' )
->fields(
Field::inst( 'mtp_event_schedule_items.item_id' )->set( false ),
Field::inst( 'mtp_event_schedule_items.item_datetime_start' )
->validator( Validate::dateFormat( 'Y-m-d H:i:s' ) )
->getFormatter( Format::datetime( 'Y-m-d H:i:s', 'Y-m-d H:i:s' ) )
->setFormatter( Format::datetime( 'Y-m-d H:i:s', 'Y-m-d H:i:s' ) ),
Field::inst( 'mtp_event_schedule_items.item_datetime_end' )
->validator( Validate::dateFormat( 'Y-m-d H:i:s' ) )
->getFormatter( Format::datetime( 'Y-m-d H:i:s', 'Y-m-d H:i:s' ) )
->setFormatter( Format::datetime( 'Y-m-d H:i:s', 'Y-m-d H:i:s' ) ),
Field::inst( 'mtp_event_schedule_items.item_speaker_id' ),
Field::inst( 'mtp_event_schedule_items.item_track_id' ),
Field::inst( 'mtp_event_schedule_items.item_track_title' ),
Field::inst( 'mtp_event_schedule_items.item_description' ),
Field::inst( 'mtp_event_schedule_items.item_track_merged' ),
Field::inst( 'mtp_event_schedule_items.item_component_type' ),
Field::inst( 'mtp_event_schedule_items.item_link' ),
Field::inst( 'mtp_event_schedule_items_meta.day_id' )
)
->leftJoin( 'mtp_event_schedule_items_meta', 'mtp_event_schedule_items.item_id', '=', 'mtp_event_schedule_items_meta.item_id' )
->where('mtp_event_schedule_items_meta.day_id', $day_id )
->debug(true)
->process( $_POST )
->json();
// load model
$this->load_model('schedule_model');
// get variables
$day_id = $_POST('day_id');
$item_id = 123;
// Add a reference table for this new
$this->my_model->add_items_meta_reference($day_id, $item_id);
Answers
Take a look at this and in particular at "postCreate". That will give you the id of the newly created row.
https://editor.datatables.net/manual/php/events