在Yii框架中给出一个按钮中的两个功能

give two function in one button in Yii framework

本文关键字:按钮 功能 两个 一个 Yii 框架      更新时间:2023-09-26

我有一个关于Yii框架的问题,我有提交按钮的问题,我想在一个提交按钮中给出两个fungsi保存和更新,谁能告诉我如何在表单上设置该功能?

<div class="row buttons">
    <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>

我用'Update'更改'Save',它仍然有错误添加主键,我如何创建两个功能更新和保存在一个按钮?

    public function actionCreate()
{
    $model=new TblUasUts;
    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);
    if(isset($_POST['TblUasUts']))
    {
        $model->attributes=$_POST['TblUasUts'];
        if($model->save())
            $this->redirect(array('view','id'=>$model->nim_mhs));
    }
            if(isset($_POST['TblUasUts'])
    {
            $model->attributes=$_POST['TblUasUts'];
            if($model->update())
            $this->redirect(array('view','id'=>$model->nim_mhs));
     }                
    $this->render('update',array(
        'model'=>$model,
    ));
}

在您的表单中,您可以使用如下内容:

<div class="row buttons">
    <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Update'); ?>
</div>

至于在后端代码上处理不同的操作,有几个选项,例如,您可以:-

  • 将表单指向不同的url
  • 设置一个(隐藏的)字段(例如ID)并解析该字段
  • 使用activeForm的默认动作,它指向调用动作,例如actionCreate()或actionUpdate()

根据您的更新,请按照我最初的建议扩展您的控制器,使其具有另一个动作actionUpdate()

actionCreate()或actionUpdate()操作之间的主要区别在于Create操作创建一个新的(空的)TblUasUts对象,而Update操作从数据库中填充TblUasUts对象。

public function actionCreate()
{
    $model=new TblUasUts;
    ...
    ... Do things with $model ...
    ...
    $model->save();
}
public function actionUpdate
{
    // The id of the existing entry is passed in the url. for example
    // ...http:// .... /update/id/10
    //
    $model = TblUasUts::model()->findByPK($_GET['id']);
    ...
    ... Do things with $model ...
    ...
    $model->save();
}