简体   繁体   English

在yii2中单击按钮时,如何将数据从一个表保存到另一表?

[英]How to save the data from one table to another table when button is clicked in yii2?

I have 3 table (in database phpmyadmin) which are: appointment , approveAppointment and rejectAppointment . 我有3个表(在数据库phpmyadmin中)是: appointmentapproveAppointmentrejectAppointment All of the column names inside each table is the same. 每个表内的所有列名称均相同。

I'm currently develop an appointment management system between student and lecturer where when student book for appointment in (frontend/view/appointment/create) all the data will be insert in appointment table in db. 我目前正在开发学生和讲师之间的约会管理系统,当要预订的学生用书(前端/视图/约会/创建)时,所有数据都将插入到db的约会表中。

Lecturer will get all the data of the appointment make by student to them the data will retrieve from table 'appointment' and shown in (frontend/view/appointment-confirmation/index using crud as in figure 1) and the lecturer need to view and will make a confirmation either approve or reject by clicking the button (shown in figure 2) 讲师将从学生那里获得所有预约的数据,这些数据将从表“约会”中检索并显示在(前端/视图/约会确认/索引,使用图1中的crud),讲师需要查看和查看单击按钮将确认是批准还是拒绝(如图2所示)

when lecturer click button approve, i want all the data about the appointment insert to table 'approveAppointment'. 当讲师单击按钮批准时,我希望将有关约会插入的所有数据添加到表“ approveAppointment”中。 if the lecturer click button reject, all the data insert to table 'rejectAppointment'. 如果讲师的单击按钮被拒绝,则所有数据都将插入到表“ rejectAppointment”中。

figure 1 图1 在此处输入图片说明

figure 2 图2 在此处输入图片说明

so this is my code for appointment-confirmation controller (actionApprove) : 所以这是我的约会确认控制器(actionApprove)的代码:

 public function actionApprove($id)
{
    $model = new ApproveAppointment();
    $model->save();
}

this is code for approveAppointment controller 这是approveAppointment控制器的代码

public function actionApproveAppointment()
{
    if (Yii::$app->request->isPost && Yii::$app->request->isAjax) {
    $appointment_id = Yii::$app->request->post('appID');
    $appointmentModel = $this->findAppointmentModel($appointment_id);

    //model instance for the approve model
    $model = new ApproveAppointment();
    $model->attributes=$appointmentModel->attributes;

    //set the response format
    Yii::$app->response->format = \yii\base\Response::FORMAT_JSON;

    $response['success'] = false;

    try {
        $model->approveAppointment();
        $response['success'] = true;
        $response['message'] = "Appointment is approved";
    } catch (\Exception $e) {
        $response['message'] = $e->getMessage();
    }
    return $response;
}

this is code for my view 这是我的看法

$appID = $model->appID;
$js = <<<JS
$("#approve").on('click',function(){
    let data=[];
    data.push({name:"appID",value:$appID});
    data.push({name:yii.getCsrfParam(),value:yii.getCsrfToken()});
    $.ajax({
        url: "index.php?r=appointment-confirmation/approveAppointment",
        data:data,
        type:'POST',
        dataType:'json',

    }).done(function( data ) {
          //display an alert or insert inside the html div for showing        

    messages
         alert(data.message);
    });
});

  JS;
  $this->registerJs($js, \yii\web\View::POS_READY);

and the approve button inside my view 和我视图中的批准按钮

<?= Html::a('Approve', ['approve', 'id'=>"approve"], ['class' => 'btn btn-primary']) ?>

WHY YOU NEED 3 similar TABLES? 为什么需要3张类似的桌子? you can make "status" column in appointment table and give value "approved","rejected","review"! 您可以在约会表中创建“状态”列,并给值“批准”,“拒绝”,“审核”!

You havent added the code for the Approve button and Reject Buttons neither you have added the model name so i will assume that you have the following model names change them accordingly 您尚未添加“批准”按钮和“拒绝”按钮的代码,也未添加模型名称,因此我将假定您具有以下模型名称,请相应地对其进行更改

  • Appointment . Appointment
  • ApproveAppointment . ApproveAppointment
  • RejectAppointment . RejectAppointment

Now about the requirements all you have to do is use the following general way 现在,关于需求的所有事情就是使用以下一般方法

$copyToModel->attributes=$copyFromModel->attributes

This will automatically copy all the values from one model to another then you can save them by calling the save() method. 这将自动将所有值从一个模型复制到另一个模型,然后您可以通过调用save()方法保存它们。 i will add the code for actionApproveAppointment only. 我将仅添加actionApproveAppointment的代码。

You can use $.ajax() or $.post() to submit the id of the appointment to the action add below to the top of your view file i am assuming you have the app_id column in the model that you are using in the appointment view page that you have shown inside the image. 您可以使用$.ajax()$.post()将约会的ID提交到视图文件顶部下面添加的操作中,我假设您在模型中使用的模型中有app_id列您在图片内显示的约会视图页面。 Just add an id="approve" to your button Approve in the html. 只需在HTML中的按钮id="approve"添加一个id="approve"

$app_id = $model->app_id;
$js = <<<JS
    $("#approve").on('click',function(){
        let data=[];
        data.push({name:"app_id",value:$app_id});
        data.push({name:yii.getCsrfParam(),value:yii.getCsrfToken()});
        $.ajax({
            url: "index.php?r=appointment-confirmation/approve-appointment",
            data:data,
            type:'POST',
            dataType:'json',

        }).done(function( data ) {
              //display an alert or insert inside the html div for showing messages
             alert(data.message);
        });
    });

JS;
$this->registerJs($js, \yii\web\View::POS_READY);

Add the below inside your ApproveAppointmentController 将以下内容添加到您的ApproveAppointmentController

public function actionApproveAppointment()
{
    if (Yii::$app->request->isPost && Yii::$app->request->isAjax) {
        $appointment_id = Yii::$app->request->post('app_id');
        $appointmentModel = $this->findAppointmentModel($appointment_id);

        //model instance for the approve model
        $model = new ApproveAppointment();
        $model->attributes=$appointmentModel->attributes;

        //set the response format
        Yii::$app->response->format = \yii\base\Response::FORMAT_JSON;

        $response['success'] = false;

        try {
            $model->approveAppointment();
            $response['success'] = true;
            $response['message'] = "Appointment is approved";
        } catch (\Exception $e) {
            $response['message'] = $e->getMessage();
        }
        return $response;
    }
}

protected function findAppointmentModel( $id ) {
    if ( ($model = Appointment::findOne ( $id )) !== null ) {
        return $model;
    } else {
        throw new NotFoundHttpException ( 'The requested page does not exist.' );
    }
}

Add the below inside your ApproveAppointment model 将以下内容添加到您的ApproveAppointment模型中

public function approveAppointment(){
    if(!$this->save()){
        throw new \Exception(implode("<br />", ArrayHelper::getColumn($this->errors, 0)));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将单击/选定的行数据从一个表传递到另一个并排放置的表 - How to pass clicked/selected row data from one table to another table that are placed side by side 单击时如何将图像从一个表的单元格移动到另一张表的单元格 - How to move image from one table's cell to another table's cell when clicked 如何在YII2中单击按钮后获取表格数组 - how to get array of table after click button in YII2 如何从数据表按钮保存信息 - how to save information from data table button 在 reactjs 中单击时,如何将按钮从一个 div 移动到另一个? - How to move a button from one div to another when clicked in reactjs? 如何在按钮上单击时创建幻灯片效果 - How to create slide effect on button when clicked on one from another 如何在表中单击按钮时隐藏进度条,数据是从服务器动态获取的(循环重复) - How to hide a progress bar when a button is clicked in a table,Data is dynamically got from server(loop repetition) 单击按钮时如何将表中的某些td转换为文本框 - How to convert some td from a table into textbox when a button is clicked 单击删除按钮时如何从表中获取行值? - How to get row value from table when delete button is clicked? 将数据复制到另一个表时切换一个表中的按钮 - Toggle button in one table when its data is copied to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM