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'); + } } }