举两个功能在Yii框架一个按钮 [英] give two function in one button in Yii framework

查看:159
本文介绍了举两个功能在Yii框架一个按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 < D​​IV CLASS =行按钮>
    < PHP的回声了CHtml ::提交按钮($建模> isNewRecord'创建'?'保存')?; ?>
< / DIV>

我改变保存与更新,它仍然有错误主键添加,我怎么可以创建两个功能更新,并保存在一个按钮?

 公共职能actionCreate()
{
    $模式=新TblUasUts;    //取消注释以下行,如果需要AJAX验证
    // $这个 - > performAjaxValidation($模型);    如果(使用isset($ _ POST ['TblUasUts']))
    {
        $建模>属性= $ _ POST ['TblUasUts'];
        如果($建模>保存())
            $这个 - >重定向(阵列('查看','ID'=> $建模> nim_mhs));
    }
            如果(使用isset($ _ POST ['TblUasUts'])
    {
            $建模>属性= $ _ POST ['TblUasUts'];
            如果($建模>更新())
            $这个 - >重定向(阵列('查看','ID'=> $建模> nim_mhs));
     }
    $这个 - >渲染('更新',阵列(
        模式= GT; $模式,
    ));
}


解决方案

在表单中,您可以使用类似:

 < D​​IV CLASS =行按钮>
    < PHP的回声了CHtml ::提交按钮($建模> isNewRecord'创建'?'更新')?; ?>
< / DIV>

至于在后端code处理不同的动作,有几个选项,例如,你可以: -


  • 直接您的形式不同网址

  • 设置(隐藏)字段(例如ID)并解析了。

  • 使用从的ActiveFor​​m,它指导回调用行动的默认操作,例如actionCreate(),或actionUpdate()

在您更新的光,请延长您的控制器按我最初的建议有另一个动作actionUpdate()

actionCreate(),或actionUpdate()操作之间的主要区别在于,创建动作创建新的(空)TblUasUts对象,而更新动作填充TblUasUts从数据库对象

 公共职能actionCreate()
{
    $模式=新TblUasUts;
    ...
    ......做事情$模型...
    ...
    $建模>保存();}公共职能actionUpdate
{
    //现有条目的ID在url中传递。例如
    // ... HTTP:// ... /更新/ ID / 10
    //
    $模式= TblUasUts ::模型() - GT; findByPK($ _ GET ['身份证']);
    ...
    ......做事情$模型...
    ...
    $建模>保存();}

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

解决方案

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

}

这篇关于举两个功能在Yii框架一个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆