/** * Returns the data model based on the primary key given in the GET variable. * If the data model is not found, an HTTP exception will be raised. * @param integer the ID of the model to be loaded */ public function loadModel($id) { $model = Credits::model()->findByPk($id); if ($model === null) { throw new CHttpException(404, 'The requested page does not exist.'); } return $model; }
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; }