Skip to content

Commit

Permalink
✨(repository): 新增判断记录是否存在
Browse files Browse the repository at this point in the history
  • Loading branch information
Joycezhangw committed Feb 7, 2023
1 parent 8bd3206 commit 856b8ed
Showing 1 changed file with 102 additions and 29 deletions.
131 changes: 102 additions & 29 deletions src/Repositories/BaseRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,11 @@ public function resetModel()

/**
* 根据主键查询
* @param $id
* @param array $columns
* @param int | string $id 组键 id
* @param array $columns 查询字段
* @return mixed
* @throws RepositoryException
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function find($id, $columns = ['*'])
{
Expand All @@ -110,10 +112,12 @@ public function find($id, $columns = ['*'])

/**
* 按字段值查询单条数据
* @param $field
* @param null $value
* @param array $columns
* @param string $field 要查询字段名
* @param null $value 字段值
* @param array $columns 查询字段
* @return mixed
* @throws RepositoryException
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function findByField($field, $value = null, $columns = ['*'])
{
Expand All @@ -124,10 +128,12 @@ public function findByField($field, $value = null, $columns = ['*'])

/**
* 按字段值查询列表
* @param $field
* @param null $value
* @param array $columns
* @param string $field 字段名
* @param null $value 字段值
* @param array $columns 查询字段
* @return mixed
* @throws RepositoryException
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function findAllByField($field, $value = null, $columns = ['*'])
{
Expand All @@ -136,13 +142,13 @@ public function findAllByField($field, $value = null, $columns = ['*'])
return $model;
}


/**
* 根据条件查询数据
* @param array $where
* @param array $columns
* @param array $where 查询条件
* @param array $columns 查询字段
* @return mixed
* @throws RepositoryException
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function findWhere(array $where, $columns = ['*'])
{
Expand All @@ -154,10 +160,12 @@ public function findWhere(array $where, $columns = ['*'])

/**
* 根据字段多个值获取数据列表
* @param string $field
* @param array $values
* @param array $columns
* @return mixed|void
* @param string $field 字段名
* @param array $values 字段值
* @param array $columns 查询字段
* @return mixed
* @throws RepositoryException
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function findWhereIn(string $field, array $values, $columns = ['*'])
{
Expand All @@ -168,10 +176,12 @@ public function findWhereIn(string $field, array $values, $columns = ['*'])

/**
* 查询不在指定字段值中的数据
* @param string $field
* @param array $values
* @param array $columns
* @return mixed|void
* @param string $field 字段名
* @param array $values 字段值
* @param array $columns 查询字段
* @return mixed
* @throws RepositoryException
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function findWhereNotIn(string $field, array $values, $columns = ['*'])
{
Expand All @@ -180,6 +190,36 @@ public function findWhereNotIn(string $field, array $values, $columns = ['*'])
return $model;
}

/**
* 判断数据是否存在,存在返回true 不存在返回false
* @param array $where 查询条件
* @return boolean true | false
* @throws RepositoryException
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function existsWhere(array $where)
{
$this->applyConditions($where);
$model = $this->model->exists();
$this->resetModel();
return $model;
}

/**
* 判断数据是否存在,存在返回 false 不存在返回 true
* @param array $where
* @return mixed
* @throws RepositoryException
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function doesntExistWhere(array $where)
{
$this->applyConditions($where);
$model = $this->model->doesntExist();
$this->resetModel();
return $model;
}


/**
* 根据主键查询一条数据
Expand All @@ -193,9 +233,10 @@ public function getByPkId(int $id)

/**
* 根据条件,获取一条指定字段数据
* @param array $condition 查询条件
* @param array $columns 查询指定字段
* @return mixed
* @throws RepositoryException
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function first(array $columns = ['*'])
{
Expand All @@ -208,6 +249,8 @@ public function first(array $columns = ['*'])
* 查找第一条数据,获取创建
* @param array $attributes
* @return mixed
* @throws RepositoryException
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function firstOrCreate(array $attributes = [])
{
Expand All @@ -230,6 +273,8 @@ public function firstOrFail(array $condition)
* 查询数据列表
* @param array $columns
* @return mixed
* @throws RepositoryException
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function all(array $columns = ['*'])
{
Expand Down Expand Up @@ -288,8 +333,10 @@ public function orderBy($column, $direction = 'asc')
/**
* 获取分页
* @param array $columns
* @param null $limit
* @param int $limit
* @return mixed
* @throws RepositoryException
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function paginate($columns = ['*'], $limit = 0)
{
Expand All @@ -299,11 +346,12 @@ public function paginate($columns = ['*'], $limit = 0)
return $results;
}


/**
* 保存新模型,此方法会返回模型实例,需要在模型上指定 fillable 或 guarded 属性
* @param array $attributes 需要保存的字段和值
* @return mixed
* @throws RepositoryException
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function create(array $attributes)
{
Expand Down Expand Up @@ -332,6 +380,8 @@ public function createBatch(array $attributes)
* @param array $attributes 要更新的字段
* @param int $id 更新主键值
* @return mixed
* @throws RepositoryException
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function updateById(array $attributes, int $id)
{
Expand All @@ -345,7 +395,9 @@ public function updateById(array $attributes, int $id)
* 根据指定条件更新数据,批量更新
* @param array $condition 更新条件
* @param array $attributes 要更新的字段
* @return mixed
* @return bool|mixed
* @throws RepositoryException
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function updateByWhere(array $condition, array $attributes)
{
Expand All @@ -361,6 +413,8 @@ public function updateByWhere(array $condition, array $attributes)
* @param string $filedName 字段名称
* @param string $fieldValue 字段值
* @return mixed
* @throws RepositoryException
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function updateFieldById(int $id, string $filedName, string $fieldValue)
{
Expand All @@ -376,6 +430,8 @@ public function updateFieldById(int $id, string $filedName, string $fieldValue)
* @param array $attributes
* @param array $values
* @return mixed
* @throws RepositoryException
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function updateOrCreate(array $attributes, array $values = [])
{
Expand Down Expand Up @@ -480,9 +536,11 @@ public function deleteWhere(array $condition)
* 1.不建议使用 $columns='*',请指定特定字段名,如果没指定,默认为主键字段名
* 2.不建议用 count() 来判断数据存不存在,请使用find 或者 first 来判断数据是否存在
*
* @param array $condition 查询条件
* @param string $columns 统计字段
* @param array $condition
* @param string $columns
* @return int
* @throws RepositoryException
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function count(array $condition = [], string $columns = ''): int
{
Expand All @@ -500,6 +558,8 @@ public function count(array $condition = [], string $columns = ''): int
* @param array $condition
* @param string $columns
* @return mixed
* @throws RepositoryException
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function sum(array $condition = [], string $columns = '')
{
Expand All @@ -517,6 +577,8 @@ public function sum(array $condition = [], string $columns = '')
* @param array $condition
* @param string $columns
* @return mixed
* @throws RepositoryException
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function avg(array $condition = [], string $columns = '')
{
Expand All @@ -534,6 +596,8 @@ public function avg(array $condition = [], string $columns = '')
* @param array $condition
* @param string $columns
* @return mixed
* @throws RepositoryException
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function max(array $condition = [], string $columns = '')
{
Expand All @@ -551,6 +615,8 @@ public function max(array $condition = [], string $columns = '')
* @param array $condition
* @param string $columns
* @return mixed
* @throws RepositoryException
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function min(array $condition = [], string $columns = '')
{
Expand All @@ -568,7 +634,9 @@ public function min(array $condition = [], string $columns = '')
* @param array $condition 条件
* @param string $filedName 指定字段名
* @param int $amount 自增数量
* @return mixed
* @return int|mixed
* @throws RepositoryException
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function increment(array $condition, string $filedName, int $amount = 1)
{
Expand All @@ -585,7 +653,9 @@ public function increment(array $condition, string $filedName, int $amount = 1)
* @param array $condition 条件
* @param string $filedName 指定字段名
* @param int $amount 递减数量
* @return mixed
* @return int|mixed
* @throws RepositoryException
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function decrement(array $condition, string $filedName, int $amount = 1)
{
Expand Down Expand Up @@ -627,6 +697,8 @@ public function parseDataRows(array $rows): array
* @param array $condition 查询条件
* @param string $key 索引
* @return array
* @throws RepositoryException
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function column(string $column, $condition = [], string $key = ''): array
{
Expand Down Expand Up @@ -669,24 +741,25 @@ public function withCount(array $relations)
// TODO: Implement withCount() method.
}


/**
* 同步关联
* @param $id
* @param $relation
* @param $attributes
* @param bool $detaching
* @return mixed
* @throws RepositoryException
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function sync($id, $relation, $attributes, $detaching = true)
{
return $this->find($id)->{$relation}()->sync($attributes, $detaching);
}


/**
* 将 where 查询条件,追加到模型
* @param array $where
* @throws RepositoryException
*/
protected function applyConditions(array $where)
{
Expand Down

0 comments on commit 856b8ed

Please sign in to comment.