修正driver类 Model类get和all方法支持闭包查询

This commit is contained in:
thinkphp
2016-04-10 16:17:06 +08:00
parent 4aa86efc5a
commit e39b72c21c
2 changed files with 18 additions and 5 deletions

View File

@@ -604,12 +604,18 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
/** /**
* 查找单条记录 * 查找单条记录
* @access public * @access public
* @param array $data 主键值 * @param mixed $data 主键值或者查询条件(闭包)
* @param bool $cache 是否缓存 * @param bool $cache 是否缓存
* @return mixed * @return mixed
*/ */
public static function get($data = '', $cache = false) public static function get($data = '', $cache = false)
{ {
$db = self::db();
if ($data instanceof \Closure) {
call_user_func_array($data, [ & $db]);
$data = [];
}
if ($cache) { if ($cache) {
// 查找是否存在缓存 // 查找是否存在缓存
$name = basename(str_replace('\\', '/', get_called_class())); $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) { if ($cache) {
// 缓存模型数据 // 缓存模型数据
@@ -632,13 +638,18 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
/** /**
* 查找所有记录 * 查找所有记录
* @access public * @access public
* @param mixed $data 主键列表 * @param mixed $data 主键列表或者查询条件(闭包)
* @param string $with 关联预查询 * @param string $with 关联预查询
* @return mixed * @return mixed
*/ */
public static function all($data = [], $with = '') 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);
} }
/** /**

View File

@@ -2267,7 +2267,9 @@ abstract class Driver
} else { } else {
// 未注册监听则记录到日志中 // 未注册监听则记录到日志中
Log::record('[ SQL ] ' . $this->queryStr . ' [ RunTime:' . $runtime . 's ]', 'sql'); 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');
}
} }
} }