格式化 start、base、traits 和 App、Config、Error、Loader 代码的样式。其他代码的样式待处理。

This commit is contained in:
Lin07ux
2017-11-05 15:21:42 +08:00
committed by ThinkPHP
parent 0fb51680e8
commit 35e763c8ff
9 changed files with 682 additions and 513 deletions

View File

@@ -10,7 +10,6 @@ use think\Model;
*/
trait SoftDelete
{
/**
* 判断当前实例是否被软删除
* @access public
@@ -19,22 +18,18 @@ trait SoftDelete
public function trashed()
{
$field = $this->getDeleteTimeField();
if (!empty($this->data[$field])) {
return true;
}
return false;
return !empty($this->data[$field]);
}
/**
* 查询软删除数据
* 查询包含软删除数据
* @access public
* @return Query
*/
public static function withTrashed()
{
$model = new static();
$field = $model->getDeleteTimeField(true);
return $model->getQuery();
return (new static)->getQuery();
}
/**
@@ -46,31 +41,28 @@ trait SoftDelete
{
$model = new static();
$field = $model->getDeleteTimeField(true);
return $model->getQuery()
->useSoftDelete($field, ['not null', '']);
return $model->getQuery()->useSoftDelete($field, ['not null', '']);
}
/**
* 删除当前的记录
* @access public
* @param bool $force 是否强制删除
* @param bool $force 是否强制删除
* @return integer
*/
public function delete($force = false)
{
if (false === $this->trigger('before_delete', $this)) {
return false;
}
if (false === $this->trigger('before_delete', $this)) return false;
$name = $this->getDeleteTimeField();
if (!$force) {
// 软删除
$this->data[$name] = $this->autoWriteTimestamp($name);
$result = $this->isUpdate()->save();
} else {
// 删除条件
$where = $this->getWhere();
// 删除当前模型数据
$result = $this->getQuery()->where($where)->delete();
// 强制删除当前模型数据
$result = $this->getQuery()->where($this->getWhere())->delete();
}
// 关联删除
@@ -78,47 +70,48 @@ trait SoftDelete
foreach ($this->relationWrite as $key => $name) {
$name = is_numeric($key) ? $name : $key;
$model = $this->getAttr($name);
if ($model instanceof Model) {
$model->delete($force);
}
if ($model instanceof Model) $model->delete();
}
}
$this->trigger('after_delete', $this);
// 清空原始数据
$this->origin = [];
return $result;
}
/**
* 删除记录
* @access public
* @param mixed $data 主键列表 支持闭包查询条件
* @param mixed $data 主键列表(支持闭包查询条件)
* @param bool $force 是否强制删除
* @return integer 成功删除的记录数
*/
public static function destroy($data, $force = false)
{
if (is_null($data)) return 0;
// 包含软删除数据
$query = self::withTrashed();
if (is_array($data) && key($data) !== 0) {
$query->where($data);
$data = null;
} elseif ($data instanceof \Closure) {
call_user_func_array($data, [ & $query]);
call_user_func_array($data, [&$query]);
$data = null;
} elseif (is_null($data)) {
return 0;
}
$resultSet = $query->select($data);
$count = 0;
if ($resultSet) {
$count = 0;
if ($resultSet = $query->select($data)) {
foreach ($resultSet as $data) {
$result = $data->delete($force);
$count += $result;
}
}
return $count;
}
@@ -130,11 +123,13 @@ trait SoftDelete
*/
public function restore($where = [])
{
$name = $this->getDeleteTimeField();
if (empty($where)) {
$pk = $this->getPk();
$where[$pk] = $this->getData($pk);
}
$name = $this->getDeleteTimeField();
// 恢复删除
return $this->getQuery()
->useSoftDelete($name, ['not null', ''])
@@ -146,30 +141,32 @@ trait SoftDelete
* 查询默认不包含软删除数据
* @access protected
* @param Query $query 查询对象
* @return void
* @return Query
*/
protected function base($query)
{
$field = $this->getDeleteTimeField(true);
$query->useSoftDelete($field);
return $query->useSoftDelete($this->getDeleteTimeField(true));
}
/**
* 获取软删除字段
* @access public
* @param bool $read 是否查询操作 写操作的时候会自动去掉表别名
* @param bool $read 是否查询操作(写操作的时候会自动去掉表别名)
* @return string
*/
protected function getDeleteTimeField($read = false)
{
$field = property_exists($this, 'deleteTime') && isset($this->deleteTime) ? $this->deleteTime : 'delete_time';
if (!strpos($field, '.')) {
$field = '__TABLE__.' . $field;
}
$field = property_exists($this, 'deleteTime') && isset($this->deleteTime) ?
$this->deleteTime :
'delete_time';
if (!strpos($field, '.')) $field = '__TABLE__.' . $field;
if (!$read && strpos($field, '.')) {
$array = explode('.', $field);
$field = array_pop($array);
}
return $field;
}
}