改进模型类的get和all方法 第二个参数为true或者数字表示缓存参数

This commit is contained in:
thinkphp
2017-01-29 18:45:56 +08:00
parent 9a308b94e3
commit 8a06e801c8

View File

@@ -189,7 +189,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
} }
// 全局作用域 // 全局作用域
if ($baseQuery && method_exists($this, 'base')) { if ($baseQuery && method_exists($this, 'base')) {
call_user_func_array([$this, 'base'], [& self::$links[$model]]); call_user_func_array([$this, 'base'], [ & self::$links[$model]]);
} }
// 返回当前模型的数据库查询对象 // 返回当前模型的数据库查询对象
return self::$links[$model]; return self::$links[$model];
@@ -326,10 +326,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
break; break;
} }
} elseif (is_string($this->autoWriteTimestamp) && in_array(strtolower($this->autoWriteTimestamp), [ } elseif (is_string($this->autoWriteTimestamp) && in_array(strtolower($this->autoWriteTimestamp), [
'datetime', 'datetime',
'date', 'date',
'timestamp' 'timestamp',
]) ])
) { ) {
$value = $this->formatDateTime($_SERVER['REQUEST_TIME'], $this->dateFormat); $value = $this->formatDateTime($_SERVER['REQUEST_TIME'], $this->dateFormat);
} else { } else {
@@ -439,10 +439,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$value = $this->readTransform($value, $this->type[$name]); $value = $this->readTransform($value, $this->type[$name]);
} elseif (in_array($name, [$this->createTime, $this->updateTime])) { } elseif (in_array($name, [$this->createTime, $this->updateTime])) {
if (is_string($this->autoWriteTimestamp) && in_array(strtolower($this->autoWriteTimestamp), [ if (is_string($this->autoWriteTimestamp) && in_array(strtolower($this->autoWriteTimestamp), [
'datetime', 'datetime',
'date', 'date',
'timestamp' 'timestamp',
]) ])
) { ) {
$value = $this->formatDateTime(strtotime($value), $this->dateFormat); $value = $this->formatDateTime(strtotime($value), $this->dateFormat);
} else { } else {
@@ -1219,7 +1219,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
if (isset(self::$event[$this->class][$event])) { if (isset(self::$event[$this->class][$event])) {
foreach (self::$event[$this->class][$event] as $callback) { foreach (self::$event[$this->class][$event] as $callback) {
if (is_callable($callback)) { if (is_callable($callback)) {
$result = call_user_func_array($callback, [& $params]); $result = call_user_func_array($callback, [ & $params]);
if (false === $result) { if (false === $result) {
return false; return false;
} }
@@ -1275,6 +1275,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
*/ */
public static function get($data = null, $with = [], $cache = false) public static function get($data = null, $with = [], $cache = false)
{ {
if (true === $with || is_int($with)) {
$cache = $with;
$with = [];
}
$query = static::parseQuery($data, $with, $cache); $query = static::parseQuery($data, $with, $cache);
return $query->find($data); return $query->find($data);
} }
@@ -1290,6 +1294,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
*/ */
public static function all($data = null, $with = [], $cache = false) public static function all($data = null, $with = [], $cache = false)
{ {
if (true === $with || is_int($with)) {
$cache = $with;
$with = [];
}
$query = static::parseQuery($data, $with, $cache); $query = static::parseQuery($data, $with, $cache);
return $query->select($data); return $query->select($data);
} }
@@ -1309,7 +1317,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$result = $result->where($data); $result = $result->where($data);
$data = null; $data = null;
} elseif ($data instanceof \Closure) { } elseif ($data instanceof \Closure) {
call_user_func_array($data, [& $result]); call_user_func_array($data, [ & $result]);
$data = null; $data = null;
} elseif ($data instanceof Query) { } elseif ($data instanceof Query) {
$result = $data->with($with)->cache($cache); $result = $data->with($with)->cache($cache);
@@ -1332,7 +1340,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$query->where($data); $query->where($data);
$data = null; $data = null;
} elseif ($data instanceof \Closure) { } elseif ($data instanceof \Closure) {
call_user_func_array($data, [& $query]); call_user_func_array($data, [ & $query]);
$data = null; $data = null;
} elseif (is_null($data)) { } elseif (is_null($data)) {
return 0; return 0;