public function actionApply($id) { if (Yii::$app->user->can('couponGet')) { $model = $this->findModel($id); $score = User::getScore(); if ($score >= $model->requireScore) { User::reduceScore($model->requireScore); $hasCoupon = new ParticipantHasCoupon(); $hasCoupon->Participant_id = Yii::$app->user->id; $hasCoupon->coupon_id = $id; $hasCoupon->is_read = 0; $hasCoupon->is_used = 0; if ($hasCoupon->save()) { $this->redirect(['index', 'apply' => 1]); } } $this->redirect(['index', 'apply' => 2]); } else { if (Yii::$app->user->isGuest) { Yii::$app->user->loginRequired(); } else { throw new ForbiddenHttpException(Yii::t('yii', 'You are not allowed to perform this action.')); } } }
/** * Creates a new ParticipantHasCoupon model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new ParticipantHasCoupon(); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'Participant_id' => $model->Participant_id, 'coupon_id' => $model->coupon_id]); } else { return $this->render('create', ['model' => $model]); } }