web-dev-qa-db-ja.com

保存されたJModelFormのID

以下のようにコントローラーからモデルを保存しています。

$model->save($data);

モデルの保存機能は次のようになります。

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;
    }

}

コントローラーにモデルを保存した後(正常に動作しています)、保存したモデルのIDが必要な関連データを保存する必要があります。そのうちの1つが他の情報と一緒にIDを返すことを期待して次のことを試みましたが、今のところ成功していません。

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

モデルのIDを取得することはそれほど難しいことではないので、ここでは基本的なものを欠落している必要があります。

2
yetanotherse

データベースドライバーは、保存後に結果をテーブルに追加します。

したがって、次のようなものが機能するはずです:

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