I have 3 table (in database phpmyadmin) which are: appointment
, approveAppointment
and rejectAppointment
. 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.
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)
when lecturer click button approve, i want all the data about the appointment insert to table 'approveAppointment'. if the lecturer click button reject, all the data insert to table 'rejectAppointment'.
so this is my code for appointment-confirmation controller (actionApprove) :
public function actionApprove($id)
{
$model = new ApproveAppointment();
$model->save();
}
this is code for approveAppointment controller
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? 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
. ApproveAppointment
. 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. i will add the code for actionApproveAppointment
only.
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. Just add an id="approve"
to your button Approve in the 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);
Add the below inside your 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
public function approveAppointment(){
if(!$this->save()){
throw new \Exception(implode("<br />", ArrayHelper::getColumn($this->errors, 0)));
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.