示例#1
0
 /**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate()
 {
     $model = new CreditsHistory();
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['CreditsHistory'])) {
         $model->attributes = $_POST['CreditsHistory'];
         if ($model->save()) {
             $this->redirect(array('view', 'id' => $model->id));
         }
     }
     $this->render('create', array('model' => $model));
 }
示例#2
0
文件: Credits.php 项目: jwerd/coupon
 public static function deduct($user_id, $amount, $item_id)
 {
     $user_id = (int) $user_id;
     if (self::checkBalance($user_id, $item_id)) {
         $transaction = Yii::app()->db->beginTransaction();
         $history = new CreditsHistory();
         $credit = Credits::model()->find('user_id = :user_id', array(':user_id' => $user_id));
         try {
             $history_data = array('user_id' => $user_id, 'credit_item_id' => $item_id, 'type' => 'deduction', 'value' => $amount);
             $history->attributes = $history_data;
             $history->save();
             $new_balance = $credit->balance - $amount;
             $credit_data = array('balance' => $new_balance);
             $credit->attributes = $credit_data;
             $credit->save();
             $transaction->commit();
         } catch (Exception $e) {
             $transaction->rollback();
         }
         return true;
     }
     return false;
 }