From 4ca39e6ce456b1e7ea5bc428dc7eab9c7fc7ebb5 Mon Sep 17 00:00:00 2001 From: Anderson Andrade Date: Wed, 15 Jul 2015 00:40:47 -0300 Subject: [PATCH] Add scopeQuery --- .../Contracts/RepositoryInterface.php | 8 ++++ .../Repository/Eloquent/BaseRepository.php | 44 +++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/src/Prettus/Repository/Contracts/RepositoryInterface.php b/src/Prettus/Repository/Contracts/RepositoryInterface.php index a8b04083..e426d22f 100644 --- a/src/Prettus/Repository/Contracts/RepositoryInterface.php +++ b/src/Prettus/Repository/Contracts/RepositoryInterface.php @@ -101,6 +101,14 @@ public function hidden(array $fields); */ public function visible(array $fields); + /** + * Query Scope + * + * @param \Closure $scope + * @return $this + */ + public function scopeQuery(\Closure $scope); + /** * Get Searchable Fields * diff --git a/src/Prettus/Repository/Eloquent/BaseRepository.php b/src/Prettus/Repository/Eloquent/BaseRepository.php index 665b7e3d..6d596ca4 100644 --- a/src/Prettus/Repository/Eloquent/BaseRepository.php +++ b/src/Prettus/Repository/Eloquent/BaseRepository.php @@ -1,6 +1,7 @@ fieldSearchable; } + /** + * Query Scope + * + * @param \Closure $scope + * @return $this + */ + public function scopeQuery(\Closure $scope){ + $this->scopeQuery = $scope; + return $this; + } + /** * Retrieve all data of repository * @@ -234,6 +251,7 @@ public function getFieldsSearchable() public function all($columns = array('*')) { $this->applyCriteria(); + $this->applyScope(); if ( $this->model instanceof \Illuminate\Database\Eloquent\Builder ){ $results = $this->model->get($columns); @@ -255,6 +273,7 @@ public function all($columns = array('*')) public function paginate($limit = null, $columns = array('*')) { $this->applyCriteria(); + $this->applyScope(); $limit = is_null($limit) ? config('repository.pagination.limit', 15) : $limit; $results = $this->model->paginate($limit, $columns); $this->resetModel(); @@ -271,6 +290,7 @@ public function paginate($limit = null, $columns = array('*')) public function find($id, $columns = array('*')) { $this->applyCriteria(); + $this->applyScope(); $model = $this->model->findOrFail($id, $columns); $this->resetModel(); return $this->parserResult($model); @@ -287,6 +307,7 @@ public function find($id, $columns = array('*')) public function findByField($field, $value = null, $columns = array('*')) { $this->applyCriteria(); + $this->applyScope(); $model = $this->model->where($field,'=',$value)->get($columns); $this->resetModel(); return $this->parserResult($model); @@ -302,6 +323,7 @@ public function findByField($field, $value = null, $columns = array('*')) public function findWhere( array $where , $columns = array('*')) { $this->applyCriteria(); + $this->applyScope(); foreach ($where as $field => $value) { if ( is_array($value) ) { @@ -351,6 +373,8 @@ public function create(array $attributes) */ public function update(array $attributes, $id) { + $this->applyScope(); + if ( !is_null($this->validator) ) { $this->validator->with($attributes) ->setId($id) @@ -381,6 +405,8 @@ public function update(array $attributes, $id) */ public function delete($id) { + $this->applyScope(); + $_skipPresenter = $this->skipPresenter; $this->skipPresenter(true); @@ -481,6 +507,24 @@ public function skipCriteria($status = true) return $this; } + /** + * Apply scope in current Query + * + * @return $this + */ + protected function applyScope() + { + + if ( isset($this->scopeQuery) && is_callable($this->scopeQuery) ) { + + $this->model = $this->scopeQuery($this->model); + + return $this; + } + + return $this; + } + /** * Apply criteria in current Query *