内核更新
This commit is contained in:
@@ -41,7 +41,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
||||
{
|
||||
|
||||
// 数据库对象池
|
||||
private static $links = [];
|
||||
protected static $links = [];
|
||||
// 数据库配置
|
||||
protected $connection = [];
|
||||
// 当前模型名称
|
||||
@@ -85,8 +85,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
||||
protected $createTime = 'create_time';
|
||||
// 更新时间字段
|
||||
protected $updateTime = 'update_time';
|
||||
// 删除时间字段
|
||||
protected $deleteTime = 'delete_time';
|
||||
// 时间字段取出后的默认时间格式
|
||||
protected $dateFormat = 'Y-m-d H:i:s';
|
||||
// 字段类型或者格式转换
|
||||
@@ -168,10 +166,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
||||
$query->pk($this->pk);
|
||||
}
|
||||
|
||||
// 全局作用域
|
||||
if (method_exists($this, 'base')) {
|
||||
call_user_func_array([$this, 'base'], [ & $query]);
|
||||
}
|
||||
self::$links[$model] = $query;
|
||||
}
|
||||
// 返回当前模型的数据库查询对象
|
||||
@@ -275,27 +269,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
||||
*/
|
||||
public function setAttr($name, $value, $data = [])
|
||||
{
|
||||
if (is_null($value) && $this->autoWriteTimestamp && in_array($name, [$this->createTime, $this->updateTime, $this->deleteTime])) {
|
||||
if (is_null($value) && $this->autoWriteTimestamp && in_array($name, [$this->createTime, $this->updateTime])) {
|
||||
// 自动写入的时间戳字段
|
||||
if (isset($this->type[$name])) {
|
||||
$type = $this->type[$name];
|
||||
if (strpos($type, ':')) {
|
||||
list($type, $param) = explode(':', $type, 2);
|
||||
}
|
||||
switch ($type) {
|
||||
case 'datetime':
|
||||
$format = !empty($param) ? $param : $this->dateFormat;
|
||||
$value = date($format, $_SERVER['REQUEST_TIME']);
|
||||
break;
|
||||
case 'timestamp':
|
||||
$value = $_SERVER['REQUEST_TIME'];
|
||||
break;
|
||||
}
|
||||
} elseif (is_string($this->autoWriteTimestamp) && in_array(strtolower($this->autoWriteTimestamp), ['datetime', 'date', 'timestamp'])) {
|
||||
$value = date($this->dateFormat, $_SERVER['REQUEST_TIME']);
|
||||
} else {
|
||||
$value = $_SERVER['REQUEST_TIME'];
|
||||
}
|
||||
$value = $this->autoWriteTimestamp($name);
|
||||
} else {
|
||||
// 检测修改器
|
||||
$method = 'set' . Loader::parseName($name, 1) . 'Attr';
|
||||
@@ -316,6 +292,36 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 自动写入时间戳
|
||||
* @access public
|
||||
* @param string $name 时间戳字段
|
||||
* @return mixed
|
||||
*/
|
||||
protected function autoWriteTimestamp($name)
|
||||
{
|
||||
if (isset($this->type[$name])) {
|
||||
$type = $this->type[$name];
|
||||
if (strpos($type, ':')) {
|
||||
list($type, $param) = explode(':', $type, 2);
|
||||
}
|
||||
switch ($type) {
|
||||
case 'datetime':
|
||||
$format = !empty($param) ? $param : $this->dateFormat;
|
||||
$value = date($format, $_SERVER['REQUEST_TIME']);
|
||||
break;
|
||||
case 'timestamp':
|
||||
$value = $_SERVER['REQUEST_TIME'];
|
||||
break;
|
||||
}
|
||||
} elseif (is_string($this->autoWriteTimestamp) && in_array(strtolower($this->autoWriteTimestamp), ['datetime', 'date', 'timestamp'])) {
|
||||
$value = date($this->dateFormat, $_SERVER['REQUEST_TIME']);
|
||||
} else {
|
||||
$value = $_SERVER['REQUEST_TIME'];
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据写入 类型转换
|
||||
* @access public
|
||||
@@ -777,9 +783,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
||||
/**
|
||||
* 删除当前的记录
|
||||
* @access public
|
||||
* @param bool $force 是否强制删除
|
||||
* @return integer
|
||||
*/
|
||||
public function delete()
|
||||
public function delete($force = false)
|
||||
{
|
||||
if (false === $this->trigger('before_delete', $this)) {
|
||||
return false;
|
||||
@@ -960,7 +967,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
||||
*/
|
||||
public static function get($data = null, $with = [], $cache = false)
|
||||
{
|
||||
$query = self::parseQuery($data, $with, $cache);
|
||||
$query = static::parseQuery($data, $with, $cache);
|
||||
return $query->find($data);
|
||||
}
|
||||
|
||||
@@ -975,7 +982,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
||||
*/
|
||||
public static function all($data = null, $with = [], $cache = false)
|
||||
{
|
||||
$query = self::parseQuery($data, $with, $cache);
|
||||
$query = static::parseQuery($data, $with, $cache);
|
||||
return $query->select($data);
|
||||
}
|
||||
|
||||
@@ -1279,14 +1286,19 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
||||
|
||||
public function __call($method, $args)
|
||||
{
|
||||
$query = $this->db();
|
||||
// 全局作用域
|
||||
if (method_exists($this, 'base')) {
|
||||
call_user_func_array('static::base', [ & $query]);
|
||||
}
|
||||
if (method_exists($this, 'scope' . $method)) {
|
||||
// 动态调用命名范围
|
||||
$method = 'scope' . $method;
|
||||
array_unshift($args, $this->db());
|
||||
array_unshift($args, $query);
|
||||
call_user_func_array([$this, $method], $args);
|
||||
return $this;
|
||||
} else {
|
||||
return call_user_func_array([$this->db(), $method], $args);
|
||||
return call_user_func_array([$query, $method], $args);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1297,6 +1309,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
||||
self::$links[$model] = (new static())->db();
|
||||
}
|
||||
$query = self::$links[$model];
|
||||
// 全局作用域
|
||||
if (method_exists($model, 'base')) {
|
||||
call_user_func_array('static::base', [ & $query]);
|
||||
}
|
||||
return call_user_func_array([$query, $method], $params);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user