From e39b72c21cfd9611310d55e1303dd9596bd34045 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sun, 10 Apr 2016 16:17:06 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3driver=E7=B1=BB=20Model?= =?UTF-8?q?=E7=B1=BBget=E5=92=8Call=E6=96=B9=E6=B3=95=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E9=97=AD=E5=8C=85=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 19 +++++++++++++++---- library/think/db/Driver.php | 4 +++- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 2730fab8..80274326 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -604,12 +604,18 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * 查找单条记录 * @access public - * @param array $data 主键值 + * @param mixed $data 主键值或者查询条件(闭包) * @param bool $cache 是否缓存 * @return mixed */ public static function get($data = '', $cache = false) { + $db = self::db(); + if ($data instanceof \Closure) { + call_user_func_array($data, [ & $db]); + $data = []; + } + if ($cache) { // 查找是否存在缓存 $name = basename(str_replace('\\', '/', get_called_class())); @@ -620,7 +626,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess } } - $result = self::db()->find($data); + $result = $db->find($data); if ($cache) { // 缓存模型数据 @@ -632,13 +638,18 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * 查找所有记录 * @access public - * @param mixed $data 主键列表 + * @param mixed $data 主键列表或者查询条件(闭包) * @param string $with 关联预查询 * @return mixed */ public static function all($data = [], $with = '') { - return self::db()->with($with)->select($data); + $db = self::db(); + if ($data instanceof \Closure) { + call_user_func_array($data, [ & $db]); + $data = []; + } + return $db->with($with)->select($data); } /** diff --git a/library/think/db/Driver.php b/library/think/db/Driver.php index 28419703..039b39b2 100644 --- a/library/think/db/Driver.php +++ b/library/think/db/Driver.php @@ -2267,7 +2267,9 @@ abstract class Driver } else { // 未注册监听则记录到日志中 Log::record('[ SQL ] ' . $this->queryStr . ' [ RunTime:' . $runtime . 's ]', 'sql'); - Log::record('[ EXPLAIN : ' . var_export($result, true) . ' ]', 'sql'); + if (!empty($explain)) { + Log::record('[ EXPLAIN : ' . var_export($explain, true) . ' ]', 'sql'); + } } }