2

I am saving a model from controller as mentioned below.

$model->save($data);

My model save function looks as shown below.

public function save($data)
{
    $id = (!empty($data['id'])) ? $data['id'] : (int)$this->getState('rev.id');
    $state = (!empty($data['state'])) ? 1 : 0;
    $user = JFactory::getUser();

    if($id) {
        //Check the user can edit this item
        $authorised = $user->authorise('core.edit', 'com_customroi.rev.'.$id) || $authorised = $user->authorise('core.edit.own', 'com_customroi.rev.'.$id);
        if($user->authorise('core.edit.state', 'com_customroi.rev.'.$id) !== true && $state == 1){ //The user cannot edit the state of the item.
            $data['state'] = 0;
        }
    } else {
        //Check the user can create new items in this section
        $authorised = $user->authorise('core.create', 'com_customroi');
        if($user->authorise('core.edit.state', 'com_customroi.rev.'.$id) !== true && $state == 1){ //The user cannot edit the state of the item.
            $data['state'] = 0;
        }
    }

    if ($authorised !== true) {
        JError::raiseError(403, JText::_('JERROR_ALERTNOAUTHOR'));
        return false;
    }

    $table = $this->getTable();
    if ($table->save($data) === true) {
        return $id;
    } else {
        return false;
    }

}

After saving the model in controller (which is working fine), I need to save some associated data for which I need the ID of the saved model. I tried the following hoping one of them will return the ID along with other information but no success so far.

$model->getProperties();
$model->getState('rev.id');

Getting ID of an model shouldn't be that difficult so I must be missing something basic here.

2

Database driver will add the result back to the Table after saving.

So, something like next should work:

$table = $this->getTable();
if ($table->save($data) === true) {
    return $table->id;
} else {
    return false;
}

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.