@@ -99,7 +99,17 @@ public function __construct($name = '', $tablePrefix = '', $connection = '')
9999 // 数据库初始化操作
100100 // 获取数据库操作对象
101101 // 当前模型有独立的数据库连接信息
102- $ this ->db (0 , empty ($ this ->connection ) ? $ connection : $ this ->connection , true );
102+ //$this->db(0, empty($this->connection) ? $connection : $this->connection, true);
103+ $ this ->connection_args = $ connection ;
104+ }
105+
106+ //检查连接是否断开
107+ function db_instance ()
108+ {
109+ if (empty ($ this ->db ) || empty ($ this ->_db ))
110+ $ this ->db (0 , empty ($ this ->connection ) ? $ this ->connection_args : $ this ->connection , true );
111+
112+ return $ this ->db ;
103113 }
104114
105115 /**
@@ -124,7 +134,8 @@ protected function _checkTableInfo()
124134 }
125135 }
126136 // 每次都会读取数据表信息
127- $ this ->flush ();
137+ if ($ this ->db )
138+ $ this ->flush ();
128139 }
129140 }
130141
@@ -136,9 +147,9 @@ protected function _checkTableInfo()
136147 public function flush ()
137148 {
138149 // 缓存不存在则查询数据表信息
139- $ this ->db ->setModel ($ this ->name );
150+ $ this ->db_instance () ->setModel ($ this ->name );
140151 $ tableName = $ this ->getTableName ();
141- $ fields = $ this ->db ->getFields ($ tableName );
152+ $ fields = $ this ->db_instance () ->getFields ($ tableName );
142153 if (!$ fields ) {
143154 // 无法获取字段信息
144155 return false ;
@@ -337,7 +348,7 @@ public function add($data = '', $options = array(), $replace = false)
337348 return false ;
338349 }
339350 // 写入数据到数据库
340- $ result = $ this ->db ->insert ($ data , $ options , $ replace );
351+ $ result = $ this ->db_instance () ->insert ($ data , $ options , $ replace );
341352 if (false !== $ result && is_numeric ($ result )) {
342353 $ pk = $ this ->getPk ();
343354 // 增加复合主键支持
@@ -380,7 +391,7 @@ public function addAll($dataList, $options = array(), $replace = false)
380391 // 分析表达式
381392 $ options = $ this ->_parseOptions ($ options );
382393 // 写入数据到数据库
383- $ result = $ this ->db ->insertAll ($ dataList , $ options , $ replace );
394+ $ result = $ this ->db_instance () ->insertAll ($ dataList , $ options , $ replace );
384395 if (false !== $ result ) {
385396 $ insertId = $ this ->getLastInsID ();
386397 if ($ insertId ) {
@@ -403,7 +414,7 @@ public function selectAdd($fields = '', $table = '', $options = array())
403414 // 分析表达式
404415 $ options = $ this ->_parseOptions ($ options );
405416 // 写入数据到数据库
406- if (false === $ result = $ this ->db ->selectInsert ($ fields ?: $ options ['field ' ], $ table ?: $ this ->getTableName (), $ options )) {
417+ if (false === $ result = $ this ->db_instance () ->selectInsert ($ fields ?: $ options ['field ' ], $ table ?: $ this ->getTableName (), $ options )) {
407418 // 数据库插入操作失败
408419 $ this ->error = L ('_OPERATION_WRONG_ ' );
409420 return false ;
@@ -476,7 +487,7 @@ public function save($data = '', $options = array())
476487 if (false === $ this ->_before_update ($ data , $ options )) {
477488 return false ;
478489 }
479- $ result = $ this ->db ->update ($ data , $ options );
490+ $ result = $ this ->db_instance () ->update ($ data , $ options );
480491 if (false !== $ result && is_numeric ($ result )) {
481492 if (isset ($ pkValue )) {
482493 $ data [$ pk ] = $ pkValue ;
@@ -554,7 +565,7 @@ public function delete($options = array())
554565 if (false === $ this ->_before_delete ($ options )) {
555566 return false ;
556567 }
557- $ result = $ this ->db ->delete ($ options );
568+ $ result = $ this ->db_instance () ->delete ($ options );
558569 if (false !== $ result && is_numeric ($ result )) {
559570 $ data = array ();
560571 if (isset ($ pkValue )) {
@@ -625,7 +636,7 @@ public function select($options = array())
625636 return $ data ;
626637 }
627638 }
628- $ resultSet = $ this ->db ->select ($ options );
639+ $ resultSet = $ this ->db_instance () ->select ($ options );
629640 if (false === $ resultSet ) {
630641 return false ;
631642 }
@@ -813,7 +824,7 @@ public function find($options = array())
813824 return $ data ;
814825 }
815826 }
816- $ resultSet = $ this ->db ->select ($ options );
827+ $ resultSet = $ this ->db_instance () ->select ($ options );
817828 if (false === $ resultSet ) {
818829 return false ;
819830 }
@@ -1010,7 +1021,7 @@ public function getField($field, $sepa = null)
10101021 if (!isset ($ options ['limit ' ])) {
10111022 $ options ['limit ' ] = is_numeric ($ sepa ) ? $ sepa : '' ;
10121023 }
1013- $ resultSet = $ this ->db ->select ($ options );
1024+ $ resultSet = $ this ->db_instance () ->select ($ options );
10141025 if (!empty ($ resultSet )) {
10151026 if (is_string ($ resultSet )) {
10161027 return $ resultSet ;
@@ -1041,7 +1052,7 @@ public function getField($field, $sepa = null)
10411052 // 当sepa指定为true的时候 返回所有数据
10421053 $ options ['limit ' ] = is_numeric ($ sepa ) ? $ sepa : 1 ;
10431054 }
1044- $ result = $ this ->db ->select ($ options );
1055+ $ result = $ this ->db_instance () ->select ($ options );
10451056 if (!empty ($ result )) {
10461057 if (is_string ($ result )) {
10471058 return $ result ;
@@ -1524,7 +1535,7 @@ public function check($value, $rule, $type = 'regex')
15241535 */
15251536 public function procedure ($ sql , $ parse = false )
15261537 {
1527- return $ this ->db ->procedure ($ sql , $ parse );
1538+ return $ this ->db_instance () ->procedure ($ sql , $ parse );
15281539 }
15291540
15301541 /**
@@ -1541,7 +1552,7 @@ public function query($sql, $parse = false)
15411552 array_shift ($ parse );
15421553 }
15431554 $ sql = $ this ->parseSql ($ sql , $ parse );
1544- return $ this ->db ->query ($ sql );
1555+ return $ this ->db_instance () ->query ($ sql );
15451556 }
15461557
15471558 /**
@@ -1558,7 +1569,7 @@ public function execute($sql, $parse = false)
15581569 array_shift ($ parse );
15591570 }
15601571 $ sql = $ this ->parseSql ($ sql , $ parse );
1561- return $ this ->db ->execute ($ sql );
1572+ return $ this ->db_instance () ->execute ($ sql );
15621573 }
15631574
15641575 /**
@@ -1573,8 +1584,9 @@ protected function parseSql($sql, $parse)
15731584 // 分析表达式
15741585 if (true === $ parse ) {
15751586 $ options = $ this ->_parseOptions ();
1576- $ sql = $ this ->db ->parseSql ($ sql , $ options );
1587+ $ sql = $ this ->db_instance () ->parseSql ($ sql , $ options );
15771588 } elseif (is_array ($ parse )) {
1589+ $ this ->db_instance ();
15781590 // SQL预处理
15791591 $ parse = array_map (array ($ this ->db , 'escapeString ' ), $ parse );
15801592 $ sql = vsprintf ($ sql , $ parse );
@@ -1583,7 +1595,7 @@ protected function parseSql($sql, $parse)
15831595 $ prefix = $ this ->tablePrefix ;
15841596 $ sql = preg_replace_callback ("/__([A-Z0-9_-]+)__/sU " , function ($ match ) use ($ prefix ) {return $ prefix . strtolower ($ match [1 ]);}, $ sql );
15851597 }
1586- $ this ->db ->setModel ($ this ->name );
1598+ $ this ->db_instance () ->setModel ($ this ->name );
15871599 return $ sql ;
15881600 }
15891601
@@ -1674,7 +1686,7 @@ public function getTableName()
16741686 public function startTrans ()
16751687 {
16761688 $ this ->commit ();
1677- $ this ->db ->startTrans ();
1689+ $ this ->db_instance () ->startTrans ();
16781690 return ;
16791691 }
16801692
@@ -1685,7 +1697,7 @@ public function startTrans()
16851697 */
16861698 public function commit ()
16871699 {
1688- return $ this ->db ->commit ();
1700+ return $ this ->db_instance () ->commit ();
16891701 }
16901702
16911703 /**
@@ -1695,7 +1707,7 @@ public function commit()
16951707 */
16961708 public function rollback ()
16971709 {
1698- return $ this ->db ->rollback ();
1710+ return $ this ->db_instance () ->rollback ();
16991711 }
17001712
17011713 /**
@@ -1715,7 +1727,7 @@ public function getError()
17151727 */
17161728 public function getDbError ()
17171729 {
1718- return $ this ->db ->getError ();
1730+ return $ this ->db_instance () ->getError ();
17191731 }
17201732
17211733 /**
@@ -1725,7 +1737,7 @@ public function getDbError()
17251737 */
17261738 public function getLastInsID ()
17271739 {
1728- return $ this ->db ->getLastInsID ();
1740+ return $ this ->db_instance () ->getLastInsID ();
17291741 }
17301742
17311743 /**
@@ -1735,7 +1747,7 @@ public function getLastInsID()
17351747 */
17361748 public function getLastSql ()
17371749 {
1738- return $ this ->db ->getLastSql ($ this ->name );
1750+ return $ this ->db_instance () ->getLastSql ($ this ->name );
17391751 }
17401752 // 鉴于getLastSql比较常用 增加_sql 别名
17411753 public function _sql ()
@@ -1760,6 +1772,9 @@ public function getPk()
17601772 */
17611773 public function getDbFields ()
17621774 {
1775+ if (is_null ($ this ->db ))
1776+ $ this ->db_instance ();
1777+
17631778 if (isset ($ this ->options ['table ' ])) {
17641779 // 动态指定表名
17651780 if (is_array ($ this ->options ['table ' ])) {
@@ -1771,7 +1786,7 @@ public function getDbFields()
17711786 return false ;
17721787 }
17731788 }
1774- $ fields = $ this ->db ->getFields ($ table );
1789+ $ fields = $ this ->db_instance () ->getFields ($ table );
17751790 return $ fields ? array_keys ($ fields ) : false ;
17761791 }
17771792 if ($ this ->fields ) {
@@ -2006,6 +2021,7 @@ public function where($where, $parse = null)
20062021 $ parse = func_get_args ();
20072022 array_shift ($ parse );
20082023 }
2024+ $ this ->db_instance ();
20092025 $ parse = array_map (array ($ this ->db , 'escapeString ' ), $ parse );
20102026 $ where = vsprintf ($ where , $ parse );
20112027 } elseif (is_object ($ where )) {
0 commit comments