give two function in one button in Yii framework

流过昼夜 提交于 2019-12-25 18:27:39

问题


I have a question about Yii framework, i have problem with submit button, i want to given two fungsi save and update in one submit button, can anyone tell me how to set that function on form ?

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

i change 'Save' with 'Update' it's still have error Primary key added, how i can create two function update and save in one push button ?

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

回答1:


In your form, you can use something like :

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

As far as processing different actions on the backend code, there are a few options, for example, you could :-

  • Direct your form to different URLs
  • Set a (hidden) field (for example ID) and parse for that.
  • Use the default action from the activeForm, which directs back to the invoking action, for example actionCreate(), or actionUpdate()

In light of your updates, please extend your controller as per my initial suggestion to have another action actionUpdate()

The main difference between the actionCreate(), or actionUpdate() actions is that the Create action create a new (empty) TblUasUts object, while the Update action populates the TblUasUts object from the database.

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();

}


来源:https://stackoverflow.com/questions/24276763/give-two-function-in-one-button-in-yii-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!