1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Get auto-incremented content ID after save

Discussion in 'XenForo Development Discussions' started by Teapot, Feb 2, 2013.

  1. Teapot

    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 :)
     
  2. Syndol

    Syndol Guest

    If you are using a datawriter then use:

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


    Otherwise use:
    $workId = $this->_getDb()->lastInsertId();
     
    Shelley and Teapot like this.
  3. Teapot

    Teapot Well-Known Member

    That was simpler than I expected! Thank you very much, Syndol :)
     
    Syndol likes this.

Share This Page