public function saveCoupon() { if ($this->validate()) { $transaction = \Yii::$app->db->beginTransaction(); try { $couponRule = new CouponRule(); if (!$this->shippingFee) { $this->shippingFee = 0; } $couponRule->desc = $this->desc; $condition = ['total_price' => $this->total_price, 'qty' => $this->qty, 'category_id' => $this->category_id]; $result = ['type' => $this->type, 'number' => $this->number, 'shipping' => $this->shipping, 'shippingFee' => $this->shippingFee]; $couponRule->condition = json_encode($condition); $couponRule->result = json_encode($result); $couponRule->save(); for ($i = 0; $i < $this->total; $i++) { $coupon = new Coupon(); $coupon->coupon_no = uniqid(); $coupon->rule_id = $couponRule->rule_id; $coupon->status = $this->status; $coupon->start_at = $this->start_at; $coupon->end_at = $this->end_at; $coupon->star_id = $this->star_id; $coupon->save(); } $transaction->commit(); return true; } catch (\yii\base\Exception $e) { $transaction->rollback(); return false; } } }
public function getCouponRule() { return $this->hasOne(CouponRule::className(), ['rule_id' => 'rule_id']); }
protected function findRuleModel($id) { if (($model = CouponRule::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
protected function findRuleModel($id) { $couponRule = Yii::createObject(CouponRule::className()); if (($model = $couponRule::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }