mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-07 23:22:48 +08:00
Model类的useGlobalScope 属性改为动态属性 全局查询范围方法base改为动态方法 软删除属性 deleteTime属性改为动态属性,并且没有定义的话默认为delete_time 取消tableAlias属性定义 直接可以在deleteTime属性里面定义别名.字段名
This commit is contained in:
@@ -100,7 +100,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
// 验证失败是否抛出异常
|
// 验证失败是否抛出异常
|
||||||
protected $failException = false;
|
protected $failException = false;
|
||||||
// 全局查询范围
|
// 全局查询范围
|
||||||
protected static $useGlobalScope = true;
|
protected $useGlobalScope = true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 初始化过的模型.
|
* 初始化过的模型.
|
||||||
@@ -1116,8 +1116,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
*/
|
*/
|
||||||
public static function useGlobalScope($use)
|
public static function useGlobalScope($use)
|
||||||
{
|
{
|
||||||
$model = new static();
|
$model = new static();
|
||||||
static::$useGlobalScope = $use;
|
$model->useGlobalScope = $use;
|
||||||
return $model;
|
return $model;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1341,8 +1341,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
{
|
{
|
||||||
$query = $this->db();
|
$query = $this->db();
|
||||||
// 全局作用域
|
// 全局作用域
|
||||||
if (static::$useGlobalScope && method_exists($this, 'base')) {
|
if ($this->useGlobalScope && method_exists($this, 'base')) {
|
||||||
call_user_func_array('static::base', [ & $query]);
|
call_user_func_array([$this, 'base'], [ & $query]);
|
||||||
}
|
}
|
||||||
if (method_exists($this, 'scope' . $method)) {
|
if (method_exists($this, 'scope' . $method)) {
|
||||||
// 动态调用命名范围
|
// 动态调用命名范围
|
||||||
@@ -1358,10 +1358,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
public static function __callStatic($method, $params)
|
public static function __callStatic($method, $params)
|
||||||
{
|
{
|
||||||
$query = self::getDb();
|
$query = self::getDb();
|
||||||
$model = get_called_class();
|
$model = new static();
|
||||||
// 全局作用域
|
// 全局作用域
|
||||||
if (static::$useGlobalScope && method_exists($model, 'base')) {
|
if ($model->useGlobalScope && method_exists($model, 'base')) {
|
||||||
call_user_func_array('static::base', [ & $query]);
|
call_user_func_array([$model, 'base'], [ & $query]);
|
||||||
}
|
}
|
||||||
return call_user_func_array([$query, $method], $params);
|
return call_user_func_array([$query, $method], $params);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,8 @@ trait SoftDelete
|
|||||||
*/
|
*/
|
||||||
public function trashed()
|
public function trashed()
|
||||||
{
|
{
|
||||||
if (!empty($this->data[static::$deleteTime])) {
|
$field = $this->getDeleteTimeField();
|
||||||
|
if (!empty($this->data[$field])) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
@@ -37,7 +38,8 @@ trait SoftDelete
|
|||||||
public static function onlyTrashed()
|
public static function onlyTrashed()
|
||||||
{
|
{
|
||||||
$model = new static();
|
$model = new static();
|
||||||
return $model->db()->where(static::$deleteTime, 'exp', 'is not null');
|
$field = $this->getDeleteTimeField();
|
||||||
|
return $model->db()->where($field, 'exp', 'is not null');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -51,10 +53,9 @@ trait SoftDelete
|
|||||||
if (false === $this->trigger('before_delete', $this)) {
|
if (false === $this->trigger('before_delete', $this)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
$name = $this->getDeleteTimeField();
|
||||||
if (static::$deleteTime && !$force) {
|
if (!$force) {
|
||||||
// 软删除
|
// 软删除
|
||||||
$name = static::$deleteTime;
|
|
||||||
$this->change[] = $name;
|
$this->change[] = $name;
|
||||||
$this->data[$name] = $this->autoWriteTimestamp($name);
|
$this->data[$name] = $this->autoWriteTimestamp($name);
|
||||||
$result = $this->isUpdate()->save();
|
$result = $this->isUpdate()->save();
|
||||||
@@ -106,12 +107,10 @@ trait SoftDelete
|
|||||||
*/
|
*/
|
||||||
public function restore($where = [])
|
public function restore($where = [])
|
||||||
{
|
{
|
||||||
if (static::$deleteTime) {
|
$name = $this->getDeleteTimeField();
|
||||||
// 恢复删除
|
// 恢复删除
|
||||||
$name = static::$deleteTime;
|
return $this->isUpdate()->save([$name => null], $where);
|
||||||
return $this->isUpdate()->save([$name => null], $where);
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -120,14 +119,28 @@ trait SoftDelete
|
|||||||
* @param \think\db\Query $query 查询对象
|
* @param \think\db\Query $query 查询对象
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
protected static function base($query)
|
protected function base($query)
|
||||||
{
|
{
|
||||||
if (static::$deleteTime) {
|
$field = $this->getDeleteTimeField(true);
|
||||||
$query->where(!empty(static::$tableAlias) ?
|
$query->where($field, 'null');
|
||||||
static::$tableAlias . '.' . static::$deleteTime :
|
|
||||||
static::$deleteTime,
|
|
||||||
'null');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取软删除字段
|
||||||
|
* @access public
|
||||||
|
* @param bool $read 是否查询操作 写操作的时候会自动去掉表别名
|
||||||
|
* @return integer
|
||||||
|
*/
|
||||||
|
protected function getDeleteTimeField($read = false)
|
||||||
|
{
|
||||||
|
if (isset($this->deleteTime)) {
|
||||||
|
$field = $this->deleteTime;
|
||||||
|
} else {
|
||||||
|
$field = 'delete_time';
|
||||||
|
}
|
||||||
|
if (!$read && strpos($field, '.')) {
|
||||||
|
list($alias, $field) = explode('.', $field);
|
||||||
|
}
|
||||||
|
return $field;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user