Wednesday, April 4, 2012

[Joomla] How to get the new ID when using JTable

If you're using the JFactory::getDBO when inserting a new row in the database, you can easily access the ID of the newly inserted row via insertid() function but in most cases (specially from the backend) we use JTable to insert the rows. The question is: how to get the new id when using the JTable class? Here's how:

First, we instantiate the JTable we're going to use
$mytable = $this->getTable('some_table');
...
$myTable->store();

Next, we'll get the new ID
$db = $mytable->getDBO();
$newID = $db->insertId();

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...