Get auto-incremented content ID after save

Teapot

Well-known member
I've come across a slight problem that I can't figure out the fix to myself, and was wondering if anyone could help. Basically, my add-on has a page that needs to write to two tables - cc_work and cc_work_writing when the user submits a form. Right now, my database looks like this:
PHP:
// cc_work
'work_id'     
'work_type'   
'user_id'     
'title'         
'creation_date'
'summary'     
 
// cc_work_writing
'work_id' // this should be set to cc_work's work_id
'content'

I've got my ControllerPublic set up to write the data to both correctly, except for cc_work_writing's work_id, which I need to set to the parent's work_id value so they're linked together. The problem is that when the ControllerPublic is working, it doesn't have the first work_id available to set, as it doesn't exist yet.

How do I get the work_id after I save the first table so that I can set it correctly in the second?

Thanks for your time :)
 
If you are using a datawriter then use:

$writer->save();
$reply = $writer->getMergedData();
$workId = $reply['work_id'];


Otherwise use:
$workId = $this->_getDb()->lastInsertId();
 
Top Bottom