Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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,
    ));
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
198 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...