فهرست منبع

通过增加select 函数选项,支持主从数据库指定

stanley-king 8 سال پیش
والد
کامیت
5c1d433b38
3فایلهای تغییر یافته به همراه16 افزوده شده و 16 حذف شده
  1. 4 7
      core/framework/libraries/model.php
  2. 5 6
      data/model/user_bonus.model.php
  3. 7 3
      test/DBTest.php

+ 4 - 7
core/framework/libraries/model.php

@@ -196,7 +196,7 @@ class Model
 	 * @param array/int $options
 	 * @return null/array
 	 */
-    public function select($options=array()) 
+    public function select($options=array())
     {
         if(is_string($options) || is_numeric($options)) {
             // 默认根据主键查询
@@ -242,7 +242,7 @@ class Model
 	 * @param array/int $options
 	 * @return null/array
 	 */
-    public function getfield($col = 1) 
+    public function getfield($col = 1)
     {
     	if (intval($col)<=1) $col = 1;
         $options =  $this->parse_options();
@@ -333,7 +333,7 @@ class Model
 	 * @param string/int $options
 	 * @return null/array
 	 */
-    public function find($options=null) 
+    public function find($options=null)
     {
         if(is_numeric($options) || is_string($options)) {
             $where[$this->get_pk()] = $options;
@@ -701,15 +701,12 @@ class ModelDb
     // 查询表达式
     protected $selectSql = 'SELECT%DISTINCT% %FIELD% FROM %TABLE%%INDEX%%JOIN%%WHERE%%GROUP%%HAVING%%ORDER%%LIMIT% %UNION%';
 
-    public function select($options=array()) 
+    public function select($options=array())
     {
         if(is_mobile())
         {
             $sql = $this->buildSelectSql($options);
-            $start = microtime(true);
             $result = DB::getAll($sql,($options['lock'] === true || $options['master'] === true || defined('TRANS_MASTER')) ? 'master' : 'slave');
-            perfor_period("model",$start,"getAll");
-
             return $result;
         }
         else

+ 5 - 6
data/model/user_bonus.model.php

@@ -134,12 +134,11 @@ class user_bonusModel extends Model
                 $is_new_grab = false;
                 $is_new_bind = false;
 
-                $tm = time() - $time_out;
-                $sql = "select * from lrlz_user_bonus where type_id={$type_id} and bonus_status IN (0,1) and grab_time < {$tm} order by bonus_status asc,bonus_id asc limit 1";
-                $bonuses = Db::getAll($sql,'master');
-
-                if(empty($bonuses))    return false;
-                if(empty($bonuses[0])) return false;
+                $cond = array( 'type_id' => $type_id,
+                    'bonus_status' => array('in', array(0,1)),
+                    'grab_time' => array('lt',time() - $time_out));
+                $bonuses = $this->where($cond)->field($fields)->order('bonus_status asc,bonus_id asc')->limit(1)->select(array('lock' => true));
+                if(empty($bonuses) || empty($bonuses[0])) return false;
 
                 $bonus = $this->try_grab($bonuses[0],$type_id,$is_new_grab,$sess_id,$is_new_bind);
                 if($bonus != false) {

+ 7 - 3
test/DBTest.php

@@ -24,7 +24,7 @@ class DBTest extends PHPUnit_Framework_TestCase
 //        $sql = "select * from lrlz_user_bonus where type_id=\'{$type_id}\' and \'session_id=\'{$sess_id}\' order by bonus_status asc,bonus_id asc limit 1";
         $mobile = '18911779278';
         $sql = "select * from lrlz_member where member_mobile='{$mobile}'";
-        $items = Db::getAll($sql,'master');
+        $items = Db::getAll($sql,array('lock' => true));
 
         //$items = Db::getAll('select * from lrlz_member');
     }
@@ -57,8 +57,12 @@ class DBTest extends PHPUnit_Framework_TestCase
         //$mod_member->rollback();
         $mod_member->commit();
         $affect_rows_b = $mod_member->affected_rows();
-
-
+    }
+    public function testMaster()
+    {
+        $mod_member = Model('member');
+        //$items = $mod_member->where(array('member_mobile' => '18911779278'))->limit(false)->select(array('lock' => true));
+        $item = $mod_member->where(array('member_mobile' => '18911779278'))->find(array('lock' => true));
     }
 
     public function testConnection()