JControllerForm - get insert ID on save

3,113 views
Skip to first unread message

Nate Baldwin

unread,
Jan 26, 2012, 4:32:42 PM1/26/12
to Joomla! General Development
I'm using an extension of JControllerForm, and trying to use the
default save() method, and then run some additional scripting in my
override. If the parent::save method is creating a new row, how would
I get the insert ID to use afterwards in my override method? I'm
currently using:

public function save($key = null, $urlVar = null)
{
parent::save($key, $urlVar);
$recordID = JRequest::getInt($urlVar);
if (!$recordID) $recordID = JFactory::getDBO()->insertid();
// do more stuff here before redirecting (handle file uploads)
}

Wasn't sure if I could rely on the main record being the last one
saved by the DBO though... is there a better way to get the insert ID
from the controller or model?

Swapnil Shah

unread,
Jan 26, 2012, 4:42:01 PM1/26/12
to joomla-de...@googlegroups.com
Nate,

If you take a look at the parent::save() method. Towards the end after a successful save, the Id of the row and whether a new record was created or not is saved to the state variable/session.

I think it's tablename.id & isNew are the two variables you are interested in. If it's not tablename then it's the modelname.id.

You will have to check the syntax for correct variable name, so you can get the information from the state variable/session.

Regards,


Neil
Sent from my iPhone

> --
> You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
> To post to this group, send an email to joomla-de...@googlegroups.com.
> To unsubscribe from this group, send email to joomla-dev-gene...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/joomla-dev-general?hl=en-GB.
>

Nate Baldwin

unread,
Jan 26, 2012, 5:00:59 PM1/26/12
to Joomla! General Development
Thanks much - that pointed me in the right direction. Looking over the
end of the save function again, looks like what I'm supposed to do is,
instead of overwriting the save method, use the postSaveHook method,
since that one receives the model and data from the parent save
method. So:

public function postSaveHook($model, $validData)
{
$item = $model->getItem();
$id = $item->get('id');
// do stuff
}


On Jan 26, 4:42 pm, Swapnil Shah <swapnilsha...@gmail.com> wrote:
> Nate,
>
> If you take a look at the parent::save() method. Towards the end after a successful save, the Id of the row and whether a new record was created or not is saved to the state variable/session.
>
> I think it's tablename.id & isNew are the two variables you are interested in. If it's not tablename then it's the modelname.id.
>
> You will have to check the syntax for correct variable name, so you can get the information from the state variable/session.
>
> Regards,
>
> Neil
> Sent from my iPhone
>

Swapnil Shah

unread,
Jan 26, 2012, 5:15:26 PM1/26/12
to joomla-de...@googlegroups.com
Hey Nate,

Check the validData for your Id. I can't remember if the item is saved at the end of save method in the model. If you try to get the model to give you an item, it might give a dummy one. I will check on that when I get home in a few hours if you are still having trouble with it.

Regards,


Neil
Sent from my iPhone

Nate Baldwin

unread,
Jan 26, 2012, 5:20:11 PM1/26/12
to Joomla! General Development
The validData variable just had the cleaned form data (so, ID field
was there, but empty if adding a new record). Getting the item and
then ID from the model seemed to work for both inserts and updates, so
I think I'm good to go now. Thanks again.

On Jan 26, 5:15 pm, Swapnil Shah <swapnilsha...@gmail.com> wrote:
> Hey Nate,
>
> Check the validData for your Id. I can't remember if the item is saved at the end of save method in the model. If you try to get the model to give you an item, it might give a dummy one. I will check on that when I get home in a few hours if you are still having trouble with it.
>
> Regards,
>
> Neil
> Sent from my iPhone
>
Reply all
Reply to author
Forward
0 new messages