例如,我有以下代码
if ($result == 1) {
foreach ($records as $record) {
$request_date = $record['date'];
$request_starttime = $record['start_time'];
echo $request_date . " " . $request_starttime;
}
throw new UserException("Unfortunately, this slot is already booked,please check another slot");
} else {
//do something else
}
这里显示异常良好,但是该块中之前的回显代码未显示在页面上。
我该如何实现?
print_r($slots)
Array
(
[0] => 2018-12-12 12:45:00
)
Array
(
[0] => 2018-12-12 12:45:00
[1] => 2018-12-12 13:00:00
)
Array
(
[0] => 2018-12-12 12:45:00
[1] => 2018-12-12 13:00:00
[2] => 2018-12-12 13:15:00
)
Array
(
[0] => 2018-12-12 12:45:00
[1] => 2018-12-12 13:00:00
[2] => 2018-12-12 13:15:00
[3] => 2018-12-12 13:30:00
)
我在print_r($slots)
行的前面添加了throw new userException
。
更详细的代码块如下:
foreach ($order_item as $key => $value) {
$order = new OrderItem();
$class_info = ClassDuration::find()->where(['id' => $value['id']])->one();
$end_date = date('Y-m-d', strtotime($model->create_date));
//$p_price = $value['price'];
$order->cd_id = $value['id'];
$order->user_id = $UserId;
$order->location_id = $value['location_id'];
$order->instructor_id = $value['instructor_id'];
$order->date = $value['date1'];
$order->start_time = $value['starttime'];
$order->end_time = date("h:i",strtotime($value['endtime']));
//$order->price = $p_price * $value['q'];
$order->order_id = $model->id;
$instructor=$value['instructor_id'];
$date=$value['date1'];
$starttime =$value['starttime'];
$query = Yii::$app->db->createCommand("SELECT IF(EXISTS(SELECT * FROM `order_item` WHERE `instructor_id`='$instructor' and `date` = '$date' AND `start_time` = '$starttime'), 1, 0)");
$query1 = Yii::$app->db->createCommand("SELECT * FROM `order_item` WHERE exists(select * FROM dual where `instructor_id`='$instructor' and `date` = '$date' AND `start_time` = '$starttime')");
$records=$query1->queryAll();
$result=$query->queryScalar();
//var_dump($result);exit;
if ($result == 1) {
foreach ($records as $record) {
$request_date = $record['date'];
$request_starttime = $record['start_time'];
$slots[] = $request_date . " " . $request_starttime;
}
print_r($slots);
$userMessage = "Unfortunately, this slot is already booked,please check another slot." . implode("<br />", $slots);
//throw new UserException($userMessage);
//echo $userMessage;
}else{
//$order->save();
}
// $grand_total = $grand_total + $order->price;
$ttl_dis = $ttl_dis;
}
答案 0 :(得分:3)
您正在尝试在$records
中列出其他用户已经预订的已检索插槽,作为您在异常中显示的文本的一部分,如果正确,则不允许该异常您要显示除异常消息中提到的文本以外的其他任何文本,应在文本后面附加异常消息,然后将其与消息一起显示。
if ($result == 1) {
foreach ($records as $record) {
$request_date = $record['date'];
$request_starttime = $record['start_time'];
$slots[] = $request_date . " " . $request_starttime;
}
$userMessage = "Unfortunately, this slot is already booked,please check another slot." . implode("<br />", $slots);
throw new UserException($userMessage);
} else {
//do something else
}
更新
您应该先检查插槽,然后再在模型中保存任何内容并重定向到视图,请参见以下代码,我在checkSlots()
上添加了一个额外的功能
public function actionCheckout() {
if( Yii::$app->user->isGuest ){
return $this->redirect(['/site/login-popup']);
}
$session = Yii::$app->session;
$model = new Order();
//$profile = UserProfile::findOne(24);//(['id' => 27])->all();//->where(['id'=>Yii::$app->user->identity->id])->one();
$user = User::findOne(['id' => Yii::$app->user->identity->id]);
$profile = UserProfile::findOne(['user_id' => Yii::$app->user->identity->id]);
$billinginfo = UserBillingInfo::findOne(['user_id' => Yii::$app->user->identity->id]);
$userchildren = UserChildren::findOne(['user_id' => Yii::$app->user->identity->id]);
$modelsKids = $user->kids;
//var_dump($modelsKids);exit;
//Customer::findOne(10);
// var_dump($profile->zipcode);exit;
$model->status = "unpaid";
$model->first_name = Yii::$app->user->identity->first_name;
$model->last_name = Yii::$app->user->identity->last_name;
$model->mobile = Yii::$app->user->identity->phone;
$model->email = Yii::$app->user->identity->email;
$model->address = isset($profile->street1) ? $profile->street1 : '';
$model->city = isset($profile->city) ? $profile->city : '';
$model->state = isset($profile->state) ? $profile->state : '';
$model->post_code = isset($profile->zipcode) ? $profile->zipcode : '';
$pp = new PaypalPayment();
$st = Yii::$app->getTable;
$site_name = $st->settings('general', 'site_name');
if( Yii::$app->request->isAjax && $model->load(Yii::$app->request->post()) ){
Yii::$app->response->format = Response::FORMAT_JSON;
return ActiveForm::validate($model);
}
//$order_item = \Yii::$app->getRequest()->getCookies()->getValue('order_item');
$order_item = $session['value'];
if( count($order_item) <= 0 ){
return $this->goHome();
}
$total = 0;
for( $x = 0; $x < count($order_item); $x++ ){
// $cart_p_p = $order_item[$x]['price'];
// $total = $total + $cart_p_p * $order_item[$x]['q'];
}
if( Yii::$app->user->isGuest ){
return $this->render('checkout', [
'model' => $model,
]);
}
$UserId = Yii::$app->user->identity->id;
//check if all slots are available
$allSlotsAvailable = $this->checkSlots($order_item);
if( $model->load(Yii::$app->request->post()) && $allSlotsAvailable ){
$user = User::findOne(['id' => Yii::$app->user->identity->id]);
$profile = UserProfile::findOne(['user_id' => $user]);
if( !empty($UserProfile) ){
$profile = UserProfile::findOne(['user_id' => $user]);
} else{
$profile = new UserProfile();
}
$model->order_number = date('ymdhis');
$model->create_date = date('Y-m-d H:i:s');
$model->status = 1;
$model->create_by = $UserId;
// $model->order_amount = $total;
//var_dump($_REQUEST);
$user->phone = $_REQUEST['Order']['mobile'];
$user->first_name = $_REQUEST['Order']['first_name'];
$user->last_name = $_REQUEST['Order']['last_name'];
$profile->user_id = $user->id;
$profile->mobile = $_REQUEST['Order']['mobile'];
$profile->street1 = $_REQUEST['Order']['address'];
$profile->city = $_REQUEST['Order']['city'];
$profile->state = $_REQUEST['Order']['state'];
$profile->zipcode = $_REQUEST['Order']['post_code'];
$profile->save(false);
if( !empty($_REQUEST['Order']['kids']) ){
$model->kids = serialize($_REQUEST['Order']['kids']);
}
$model->save();
$user->save();
$model->orderUpdate($model->id, 1, NULL);
$grand_total = 0;
$ttl_dis = 0;
foreach( $order_item as $key => $value ){
$order = new OrderItem();
$class_info = ClassDuration::find()->where(['id' => $value['id']])->one();
$end_date = date('Y-m-d', strtotime($model->create_date));
//$p_price = $value['price'];
$order->cd_id = $value['id'];
$order->user_id = $UserId;
$order->location_id = $value['location_id'];
$order->instructor_id = $value['instructor_id'];
$order->date = $value['date1'];
$order->start_time = $value['starttime'];
$order->end_time = date("h:i", strtotime($value['endtime']));
//$order->price = $p_price * $value['q'];
$order->order_id = $model->id;
$order->save();
// $grand_total = $grand_total + $order->price;
$ttl_dis = $ttl_dis;
}
$model->order_amount = $grand_total;
$model->save();
$session->remove('date1');
$session->remove('time1');
Yii::$app->session->setFlash('orderPlaced');
$link = '#';
if( $model->payment_method == 'paypal' ){
$new_array = $order_item;
$pp->addMultipleItems($new_array);
return $pp->getCheckoutForm($model->id);
} elseif( $model->payment_method == 'invoice' ){
$content = $this->renderPartial('_invoice', ['model' => $model]);
//var_dump($content);
$filename = 'web/customer-invoice/invoice' . $model->id . '.pdf';
$pdf = new Pdf(['format' => Pdf::FORMAT_A4]);
$mpdf = $pdf->api;
$stylesheet = file_get_contents('themes/common/css/print/invoice.css');
$mpdf->WriteHTML($stylesheet, 1);
$mpdf->WriteHtml($content);
$mpdf->Output($filename, 'F');
$from_email = $st->settings('email', 'from_email');
$from_name = $st->settings('email', 'from_name');
$user = User::findOne($UserId);
$to = $user['email'];
$email_template = $st->email_template(10);
\Yii::$app->mailer->compose('template', ['id' => 10, 'user_id' => $UserId,
'email_template' => $email_template,
'model' => $model,
'link' => $link])
->setFrom([$from_email => $from_name])
->setTo($to)
->setSubject($email_template['subject'] . ' ' . $site_name)
->attach($filename)
->send();
} elseif( $model->payment_method == 'booking' ){
$admin = User::find()->select('email')->where(['user_role' => 'admin'])->asArray()->all();
$admin = ArrayHelper::getColumn($admin, 'email');
$content = $this->renderPartial('_booking', ['model' => $model]);
// var_dump($content);
$filename = 'web/customer-booking/booking' . $model->id . '.pdf';
$pdf = new Pdf(['format' => Pdf::FORMAT_A4]);
$mpdf = $pdf->api;
$stylesheet = file_get_contents('themes/common/css/print/invoice.css');
$mpdf->WriteHTML($stylesheet, 1);
$mpdf->WriteHtml($content);
$mpdf->Output($filename, 'F');
$from_email = $st->settings('email', 'from_email');
$from_name = $st->settings('email', 'from_name');
$user = User::findOne($UserId);
$orderid = Order::find(['created_by' => $user->id])->select('id')->orderBy('create_date DESC')->one();
$instructor_id = OrderItem::find()->select('instructor_id')->where(['order_id' => $orderid])->distinct();
$Instructor = User::findOne($instructor_id);
// var_dump($instructor_id);exit;
// $admin = ArrayHelper::getColumn($admin, 'email');
$to = $user['email'];
$instructor_email = $Instructor['email'];
$admin[] = $to;
$admin[] = $instructor_email;
// $to .= $Instructor['email'];
$email_template = $st->email_template(10);
\Yii::$app->mailer->compose('template', ['id' => 10, 'user_id' => $UserId,
'email_template' => $email_template,
'model' => $model,
'link' => $link])
->setFrom([$from_email => $from_name])
->setTo($admin)
->setSubject($email_template['subject'] . ' ' . $site_name)
->attach($filename)
->send();
}
//Yii::$app->response->cookies->remove('order_item');
unset($session['value']);
return $this->redirect(Yii::getAlias('@web') . '/order/view?id=' . $model->id);
//return $this->redirect('result');
} else{
return $this->render('checkout', [
'model' => $model,
// 'modelsKids' => (empty($modelsKids)) ? [new UserChildren] : $modelsKids,
]);
}
}
private function checkSlots($order_items) {
$slots = [];
foreach( $order_items as $item ){
$instructor = $item['instructor_id'];
$date = $item['date1'];
$starttime = $item['starttime'];
$query = Yii::$app->db->createCommand("SELECT IF(EXISTS(SELECT * FROM `order_item` WHERE `instructor_id`='$instructor' and `date` = '$date' AND `start_time` = '$starttime'), 1, 0)");
$result = $query->queryScalar();
if( $result == 1 ){
$slots[] = $date . " " . $starttime;
}
}
if( sizeof($slots) ){
Yii::$app->session->setFlash('error', "Unfortunately, this slot is already booked,please check another slot<br />".implode("<br />", $slots));
return false;
} else{
return true;
}
}
在功能checkSlots($order_items)
中,您将看到我删除了您的一个查询
$query1 = Yii::$app->db->createCommand("SELECT * FROM `order_item` WHERE exists(select * FROM dual where `instructor_id`='$instructor' and `date` = '$date' AND `start_time` = '$starttime')");
无需检索与您查询相同的date
和start_time
的记录,只需使用第一个查询的结果并将日期和开始时间添加到$slots
,并且一旦检查了所有记录并且是否有保留的插槽,该方法将返回false,并且视图将与显示所有保留的插槽的Flash消息一起显示。