Model类的useGlobalScope 属性改为动态属性 全局查询范围方法base改为动态方法 软删除属性 deleteTime属性改为动态属性,并且没有定义的话默认为delete_time 取消tableAlias属性定义 直接可以在deleteTime属性里面定义别名.字段名

This commit is contained in:
thinkphp
2016-09-30 18:14:57 +08:00
parent cf4415e9d9
commit a0b1a75d92
2 changed files with 39 additions and 26 deletions

View File

@@ -100,7 +100,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
// 验证失败是否抛出异常
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)
{
$model = new static();
static::$useGlobalScope = $use;
$model = new static();
$model->useGlobalScope = $use;
return $model;
}
@@ -1341,8 +1341,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
{
$query = $this->db();
// 全局作用域
if (static::$useGlobalScope && method_exists($this, 'base')) {
call_user_func_array('static::base', [ & $query]);
if ($this->useGlobalScope && method_exists($this, 'base')) {
call_user_func_array([$this, 'base'], [ & $query]);
}
if (method_exists($this, 'scope' . $method)) {
// 动态调用命名范围
@@ -1358,10 +1358,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
public static function __callStatic($method, $params)
{
$query = self::getDb();
$model = get_called_class();
$model = new static();
// 全局作用域
if (static::$useGlobalScope && method_exists($model, 'base')) {
call_user_func_array('static::base', [ & $query]);
if ($model->useGlobalScope && method_exists($model, 'base')) {
call_user_func_array([$model, 'base'], [ & $query]);
}
return call_user_func_array([$query, $method], $params);
}

View File

@@ -12,7 +12,8 @@ trait SoftDelete
*/
public function trashed()
{
if (!empty($this->data[static::$deleteTime])) {
$field = $this->getDeleteTimeField();
if (!empty($this->data[$field])) {
return true;
}
return false;
@@ -37,7 +38,8 @@ trait SoftDelete
public static function onlyTrashed()
{
$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)) {
return false;
}
if (static::$deleteTime && !$force) {
$name = $this->getDeleteTimeField();
if (!$force) {
// 软删除
$name = static::$deleteTime;
$this->change[] = $name;
$this->data[$name] = $this->autoWriteTimestamp($name);
$result = $this->isUpdate()->save();
@@ -106,12 +107,10 @@ trait SoftDelete
*/
public function restore($where = [])
{
if (static::$deleteTime) {
// 恢复删除
$name = static::$deleteTime;
return $this->isUpdate()->save([$name => null], $where);
}
return false;
$name = $this->getDeleteTimeField();
// 恢复删除
return $this->isUpdate()->save([$name => null], $where);
}
/**
@@ -120,14 +119,28 @@ trait SoftDelete
* @param \think\db\Query $query 查询对象
* @return void
*/
protected static function base($query)
protected function base($query)
{
if (static::$deleteTime) {
$query->where(!empty(static::$tableAlias) ?
static::$tableAlias . '.' . static::$deleteTime :
static::$deleteTime,
'null');
}
$field = $this->getDeleteTimeField(true);
$query->where($field, '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;
}
}