功能說明:提交訂單以後,增長消費記錄,扣除用戶餘額php
//更新積分值 public function saveMoney($uid, $money, $remark, $paymment, $chargetypes, $orderno, $status) { $sql = "UPDATE $this->_member_list SET money=money+$money WHERE uid=$uid"; $row = $this->exec ( $sql ); //增長曆史記錄 if ($row > 0) { return $this->add ( $this->_money_history, array ('uid' => $uid, 'coupons' => $money, 'date' => time (), 'payment' => $paymment, 'chargetypes' => $chargetypes, 'remark' => $remark, 'orderno' => $orderno, 'status' => $status ) ); } return 0; }
這個方法涉及到2個模型,用戶模型和訂單(在controller中耦合)sql
好處:假如我要替換一個用戶系統,那麼,我只須要管理MemberService服務類便可,跟訂單徹底沒有關係,用戶有沒有減去錢,跟訂單有個毛線關係,這叫作職責性。數據庫
規則:在Service類中,只能操做本身有關的數據表(按找功能導向寫)tabprefix前綴相同的 如w_member* w_order*等ui
<?php //用戶服務類 class MemberService { public function saveMoney() { //減去用戶錢包中的錢 } } class OrderController { /** * 在控制器中進行耦合 */ public function addOrder() { $money = self::getOrderService ()->cost ( $this->_user ['uid'], $_POST ['order'] ); self::getMemberService ()->saveMoney ( $this->_user ['uid'], $money ); } public static function getOrderService() { return new OrderService (); } public static function getMemberService() { return new MemberService (); } } //模型 這裏不容許有任何關於用戶的信息 這和訂單有關 class OrderService { public function cost($uid, $order) { //這裏始終要減去用戶錢包中的錢 $order = new OrderModel (); $order->addMoneyHistory ( $uid, $order ); return $order ['money']; } } /** * 數據庫操做類 */ class OrderModel { /** * 添加消費歷史記錄 * @param int $uid 用戶id * @param decimal $money 消費金額 * @param Array $order 訂單信息 */ public function addMoneyHistory($uid, $order) { $param = array ('uid' => $uid, 'coupons' => $order ['money'], 'date' => time (), 'payment' => $order ['payment'], 'chargetypes' => $order ['chargetypes'], 'remark' => $order ['remark'], 'orderno' => $order ['orderno'], 'status' => $order ['status'] ); $this->add ( $this->_money_history, $param ); } }