0

I'm using a method to store information in another table. This is how I'm doing that:

public static function CreateMail($userid, $title, $state = 1)
{           
    $maildata['userid'] = $userid;
    $maildata['title']  = $title;
    $maildata['state']  = $state;               

    $row = JTable::getInstance('Mail','BestiaTable');
    $row->bind($maildata);
    $row->check();
    $storemessage = $row->store();

    return $storemessage;                       
}

This is working fine - but one problem remains: I would like to get the id of the saved data.

How can I do that?

Edit: The given answer worked on 3.4.48 but on 3.5 $row->id always returns NULL.

3 Answers 3

4

$row contains inserted data with PK. So $row->id is id of the saved data. So you can do

 public static function CreateMail($userid, $title, $state = 1)
    {           
        $maildata['userid']     =       $userid;
        $maildata['title']      =       $title;
        $maildata['state']      =       $state;             

        $row=JTable::getInstance('Mail','BestiaTable');
        $row->bind($maildata);
        $row->check();
        $storemessage = $row->store();

        return $row->id;                       
    }
1
  • Hm, it worked fine. But since Joomla 3.5 it no longer works :-(
    – MyFault
    Mar 25, 2016 at 19:58
1

To get the last insertId use:

$newID = $row->insertId();

Here is the documentation You have to call it just after

$store = $row->store(); 
1

The problem is solved. On Joomla 3.5 the PDO driver does not return an id.

This is discussed and will be solved: https://github.com/joomla/joomla-cms/issues/9534

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.