5

We need to create a new usergroup for the installation of our component, and the below code saves the data just as expected. However, retrieving the ID of the Usergroup for later use "isn't working".

Using JTable returns the ID, but won't fire any plugins - am I missing something obvious here in retrieving the ID of the record that the model just added?

$model = new UsersModelGroup(array('table_path' => $basePath.'/tables'));
$data = array('parent_id' => '2','title'=>'My Usergroup');
if(!$model->save($data)){
    $msg = 'Usergroup was not saved: '.$model->getError();
}else{
    $my_new_group_id = $model->get('id');    // Returns "null"
}

2 Answers 2

7

Try using the postSaveHook method which allows the controller to access model data after it has been saved.

public function postSaveHook($model, $validData)
{
    $item = $model->getItem();
    $my_new_group_id = $item->get('id');
}
4
  • Drat - foiled again by the getItem() method....glad I mentioned that I knew it was going to be obvious, lol.
    – GDP
    Nov 1, 2014 at 18:25
  • Glad it helped :) Do I get flowers this time: joomla.stackexchange.com/a/4255/168 ? lol
    – Lodder
    Nov 1, 2014 at 18:27
  • 3
    I was hoping you'd forgotten that....i looked at the price of shipping to where you're at.
    – GDP
    Nov 1, 2014 at 18:28
  • 2
    The trick is to call a local flower place and have them deliver it. Much cheaper that way ;) Nov 2, 2014 at 0:15
2

Even though this is an old post. I wanted to post one of the solutions that helped me.

JLoader::import('group', JPATH_ADMINISTRATOR.'/components/com_users/models');
$groupModel = JModelLegacy::getInstance( 'Group', 'UsersModel' );
$groupModel->save($group);
$groupId = $groupModel->getState('group.id');

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.