I would like to get the id of the last saved dataset in a JTable-Operation.

At the moment I got this:

// Create and populate an object.
$value         = new stdClass();
$value->userid = (int)$userid;
$value->type   = $data['type']; 

// Insert the object into the user profile table.
$handle = JFactory::getDbo()->insertObject('#__bestia_handles', $value);

Now I would like to get the id of this saved object.

$handle only contains true or false.

How can I solve this problem?

3 Answers 3


Solved it :-)

Just define

$db                         =   JFactory::getDbo();

and after


I'm able to call

  • 1
    Please note this may not always work: See this:joomla.stackexchange.com/a/5768/168
    – Lodder
    Commented Jul 9, 2015 at 14:49
  • Thanks a lot - as far as I understood the reason for this behavior is not clear yet? In my case it works fine - but of course I would like to use a method that always works ;-)
    – MyFault
    Commented Jul 9, 2015 at 14:59
  • This problem is caused by bad MySQL configuration. You will not meet it on most of the hosts. Commented Jul 13, 2015 at 15:55

I'm having the same problem as I'm using multiple tables for my data and linking them however this seems like a good method, although one thing I don't understand though is there a reason why JTable's save method only returns true/false surely it would be better to return item id or NULL as that would be so much more useful. Of course I presume there is nothing to stop you creating an override in your own table class to do so like this...

    MyComponentTableMyTable extends JTable
        public function save($src, $orderingFilter= '', $ignore= '')
            /*call parent save function and return either insertid or null depending on whether or not parent save function returns true or false */
            return ( parent::save ( $src, $orderingFilter, $ignore ) ? $_db->insertid () : NULL ) ;

I haven't actually tried this yet but can't see any reason why it shouldn't work.


When dealing with a single row of data, try using JTable. It follows the Active Record pattern. After performing a $row->save(), $row->id is updated with the actual value of the primary key.

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.