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

pvcm50d1  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(249)

我有3个表(在数据库phpmyadmin中),它们是: appointment , approveAppointment 以及 rejectAppointment . 每个表中的所有列名都相同。
我目前正在开发一个学生和讲师之间的预约管理系统,当学生在(frontend/view/appointment/create)中预订预约时,所有数据都将插入数据库中的预约表中。
讲师将获得学生预约的所有数据,这些数据将从表'appointment'中检索并显示在(如图1所示的frontend/view/appointment confirmation/index using crud)中,讲师需要查看并通过单击按钮(如图2所示)来确认批准或拒绝
单击“批准”按钮时,我希望将有关约会的所有数据插入表“批准约会”。如果讲师单击“拒绝”按钮,则所有数据都将插入表“拒绝约会”。
图1

图2

这是我的预约确认控制器代码(actionapprove):

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

这是ApproveAppoint控制器的代码

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

这是我的观点

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

以及我视图中的“批准”按钮

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

js4nwp541#

您没有添加“批准”按钮和“拒绝”按钮的代码,也没有添加模型名称,因此我假设您有以下模型名称,并相应地更改它们 Appointment . ApproveAppointment . RejectAppointment .
现在关于需求,您所要做的就是使用以下一般方法

$copyToModel->attributes=$copyFromModel->attributes

这将自动将所有值从一个模型复制到另一个模型,然后您可以通过调用 save() 方法。我将为添加代码 actionApproveAppointment 只是。
你可以用 $.ajax() 或者 $.post() 若要将约会的id提交到操作,请将下面的添加到视图文件的顶部,我假设您拥有 app_id 显示在图像中的“约会视图”页中使用的模型中的列。只需添加一个 id="approve" 在html中单击“批准”按钮。

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

将下面的内容添加到 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.' );
}
}

将下面的内容添加到 `ApproveAppointment` 模型

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

tez616oj

tez616oj2#

为什么需要3张类似的table?您可以在预约表中设置“状态”列,并赋予值“已批准”、“已拒绝”、“已审核”!

相关问题