Commit 66b698fb authored by 王源's avatar 王源 🎧

添加with和orderby方法,新增和更新加入验证器验证

parent 8ae55d25
...@@ -24,4 +24,23 @@ interface RepositoryInterface ...@@ -24,4 +24,23 @@ interface RepositoryInterface
public function find($id, $columns = array('*')); public function find($id, $columns = array('*'));
public function findBy($field, $value, $columns = array('*')); public function findBy($field, $value, $columns = array('*'));
/**
* Load relations
*
* @param $relations
*
* @return $this
*/
public function with($relations);
/**
* Order collection by a given column
*
* @param string $column
* @param string $direction
*
* @return $this
*/
public function orderBy($column, $direction = 'asc');
} }
\ No newline at end of file
...@@ -12,6 +12,7 @@ namespace Meibuyu\Micro\Repository\Eloquent; ...@@ -12,6 +12,7 @@ namespace Meibuyu\Micro\Repository\Eloquent;
use Hyperf\DbConnection\Model\Model; use Hyperf\DbConnection\Model\Model;
use Meibuyu\Micro\Exceptions\HttpResponseException; use Meibuyu\Micro\Exceptions\HttpResponseException;
use Meibuyu\Micro\Exceptions\RepositoryException; use Meibuyu\Micro\Exceptions\RepositoryException;
use Meibuyu\Micro\Exceptions\ValidatorException;
use Meibuyu\Micro\Repository\Contracts\RepositoryInterface; use Meibuyu\Micro\Repository\Contracts\RepositoryInterface;
use Meibuyu\Micro\Validator\Contracts\ValidatorInterface; use Meibuyu\Micro\Validator\Contracts\ValidatorInterface;
use Psr\Container\ContainerInterface; use Psr\Container\ContainerInterface;
...@@ -96,7 +97,6 @@ abstract class BaseRepository implements RepositoryInterface ...@@ -96,7 +97,6 @@ abstract class BaseRepository implements RepositoryInterface
return null; return null;
} }
/** /**
* @param $id * @param $id
* @param array $columns * @param array $columns
...@@ -121,20 +121,52 @@ abstract class BaseRepository implements RepositoryInterface ...@@ -121,20 +121,52 @@ abstract class BaseRepository implements RepositoryInterface
return $this->model->get($columns); return $this->model->get($columns);
} }
public function with($relations)
{
$this->model = $this->model->with($relations);
return $this;
}
public function orderBy($column, $direction = 'asc')
{
$this->model = $this->model->orderBy($column, $direction);
return $this;
}
public function paginate($perPage = 10, $columns = ['*']) public function paginate($perPage = 10, $columns = ['*'])
{ {
return $this->model->paginate($perPage, $columns); return $this->model->paginate($perPage, $columns);
} }
/**
* @param array $attributes
* @return Model
* @throws ValidatorException
*/
public function create(array $attributes) public function create(array $attributes)
{ {
if (!is_null($this->validator)) {
$this->validator->with($attributes)->passesOrFail(ValidatorInterface::RULE_CREATE);
}
$model = $this->model->newInstance($attributes); $model = $this->model->newInstance($attributes);
$model->save(); $model->save();
return $model; return $model;
} }
/**
* @param array $attributes
* @param $id
* @return Model | mixed
* @throws ValidatorException
*/
public function update(array $attributes, $id) public function update(array $attributes, $id)
{ {
if (!is_null($this->validator)) {
$this->validator->with($attributes)->setId($id)->passesOrFail(ValidatorInterface::RULE_UPDATE);
}
$model = $this->model->findOrFail($id); $model = $this->model->findOrFail($id);
$model->fill($attributes); $model->fill($attributes);
$model->save(); $model->save();
...@@ -143,13 +175,18 @@ abstract class BaseRepository implements RepositoryInterface ...@@ -143,13 +175,18 @@ abstract class BaseRepository implements RepositoryInterface
/** /**
* @param $id * @param $id
* @return bool|null * @return bool|mixed
* @throws \Exception * @throws HttpResponseException
*/ */
public function delete($id) public function delete($id)
{ {
$model = $this->find($id); $model = $this->find($id);
return $model->delete(); $delete = $model->delete();
if ($delete !== false) {
return $delete;
} else {
throw new HttpResponseException('删除失败,请刷新重试');
}
} }
public function findBy($field, $value, $columns = ['*']) public function findBy($field, $value, $columns = ['*'])
...@@ -157,5 +194,4 @@ abstract class BaseRepository implements RepositoryInterface ...@@ -157,5 +194,4 @@ abstract class BaseRepository implements RepositoryInterface
return $this->model->where($field, '=', $value)->first($columns); return $this->model->where($field, '=', $value)->first($columns);
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment