簡體   English   中英

在yii2中單擊按鈕時,如何將數據從一個表保存到另一表?

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

我有3個表(在數據庫phpmyadmin中)是: appointmentapproveAppointmentrejectAppointment 每個表內的所有列名稱均相同。

我目前正在開發學生和講師之間的約會管理系統,當要預訂的學生用書(前端/視圖/約會/創建)時,所有數據都將插入到db的約會表中。

講師將從學生那里獲得所有預約的數據,這些數據將從表“約會”中檢索並顯示在(前端/視圖/約會確認/索引,使用圖1中的crud),講師需要查看和查看單擊按鈕將確認是批准還是拒絕(如圖2所示)

當講師單擊按鈕批准時,我希望將有關約會插入的所有數據添加到表“ approveAppointment”中。 如果講師的單擊按鈕被拒絕,則所有數據都將插入到表“ rejectAppointment”中。

圖1 在此處輸入圖片說明

圖2 在此處輸入圖片說明

所以這是我的約會確認控制器(actionApprove)的代碼:

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

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

這是我的看法

$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']) ?>

為什么需要3張類似的桌子? 您可以在約會表中創建“狀態”列,並給值“批准”,“拒絕”,“審核”!

您尚未添加“批准”按鈕和“拒絕”按鈕的代碼,也未添加模型名稱,因此我將假定您具有以下模型名稱,請相應地對其進行更改

  • Appointment
  • ApproveAppointment
  • RejectAppointment

現在,關於需求的所有事情就是使用以下一般方法

$copyToModel->attributes=$copyFromModel->attributes

這將自動將所有值從一個模型復制到另一個模型,然后您可以通過調用save()方法保存它們。 我將僅添加actionApproveAppointment的代碼。

您可以使用$.ajax()$.post()將約會的ID提交到視圖文件頂部下面添加的操作中,我假設您在模型中使用的模型中有app_id列您在圖片內顯示的約會視圖頁面。 只需在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);

將以下內容添加到您的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("<br />", ArrayHelper::getColumn($this->errors, 0)));
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM