我有3个表(在数据库phpmyadmin中)是:appointment
,approveAppointment
和rejectAppointment
。每个表内的所有列名称均相同。
我目前正在开发学生和讲师之间的约会管理系统,当要预订的学生用书(前端/视图/约会/创建)时,所有数据都将插入到db的约会表中。
讲师将获取学生对他们进行的所有约会数据,这些数据将从表“约会”中检索并显示在(前端/视图/约会确认/索引,使用图1中的crud),并且讲师需要查看并通过单击按钮进行确认(通过批准或拒绝)(如图2所示)
当讲师单击按钮批准时,我希望将有关约会的所有数据插入表“ approveAppointment”。如果讲师单击按钮被拒绝,则所有数据都将插入到表“ rejectAppointment”中。
这是我的约会确认控制器(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']) ?>
答案 0 :(得分:1)
为什么需要3张类似的桌子?您可以在约会表中创建“状态”列,并给值“批准”,“拒绝”,“审查”!
答案 1 :(得分:0)
您尚未添加“批准”按钮和“拒绝按钮”的代码,也未添加模型名称,因此我将假定您具有以下模型名称,请相应地对其进行更改
Appointment
。ApproveAppointment
。RejectAppointment
。现在,您所需要做的就是使用以下常规方法
$copyToModel->attributes=$copyFromModel->attributes
这将自动将所有值从一个模型复制到另一个模型,然后您可以通过调用save()
方法来保存它们。我将仅添加actionApproveAppointment
的代码。
您可以使用$.ajax()
或$.post()
将约会的ID提交到以下操作,该操作添加到视图文件顶部下面的操作中,我假设您在图片中显示的约会视图页面中使用的模型。只需将app_id
添加到您的HTML批准按钮中即可。
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