Skip to content

Commit

Permalink
Add scopeQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
andersao committed Jul 15, 2015
1 parent 26162d1 commit 4ca39e6
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Prettus/Repository/Contracts/RepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
44 changes: 44 additions & 0 deletions src/Prettus/Repository/Eloquent/BaseRepository.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Prettus\Repository\Eloquent;

use Closure;
use Exception;
use Prettus\Repository\Contracts\CriteriaInterface;
use Prettus\Repository\Contracts\Presentable;
Expand Down Expand Up @@ -74,6 +75,11 @@ abstract class BaseRepository implements RepositoryInterface, RepositoryCriteria
*/
protected $skipPresenter = false;

/**
* @var \Closure
*/
protected $scopeQuery = null;

/**
* @param Application $app
*/
Expand Down Expand Up @@ -225,6 +231,17 @@ public function getFieldsSearchable()
return $this->fieldSearchable;
}

/**
* Query Scope
*
* @param \Closure $scope
* @return $this
*/
public function scopeQuery(\Closure $scope){
$this->scopeQuery = $scope;
return $this;
}

/**
* Retrieve all data of repository
*
Expand All @@ -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);
Expand All @@ -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();
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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) ) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -381,6 +405,8 @@ public function update(array $attributes, $id)
*/
public function delete($id)
{
$this->applyScope();

$_skipPresenter = $this->skipPresenter;
$this->skipPresenter(true);

Expand Down Expand Up @@ -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
*
Expand Down

0 comments on commit 4ca39e6

Please sign in to comment.