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"
}
4

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');
}
  • Drat - foiled again by the getItem() method....glad I mentioned that I knew it was going to be obvious, lol. – GDP Nov 1 '14 at 18:25
  • Glad it helped :) Do I get flowers this time: joomla.stackexchange.com/a/4255/168 ? lol – Lodder Nov 1 '14 at 18:27
  • 2
    I was hoping you'd forgotten that....i looked at the price of shipping to where you're at. – GDP Nov 1 '14 at 18:28
  • 2
    The trick is to call a local flower place and have them deliver it. Much cheaper that way ;) – David Fritsch Nov 2 '14 at 0:15
1

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, privacy policy and cookie policy

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