-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from dreanmer/feature/criteria
Adding Criterias to improve db queries usability
- Loading branch information
Showing
3 changed files
with
184 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: Alexandre | ||
* Date: 29/09/2017 | ||
* Time: 13:47 | ||
*/ | ||
|
||
namespace Frogg; | ||
|
||
use Phalcon\Mvc\Model as PhalconModel; | ||
|
||
/** | ||
* Class Criteria | ||
* | ||
* default call criteria on model, example: | ||
* | ||
* public static function query(DiInterface $dependencyInjector = null) | ||
* { | ||
* return parent::query($dependencyInjector)->softDelete(); | ||
* } | ||
* | ||
* @package Frogg | ||
* @method static addSoftDelete(string $column = 'deleted', int $activeValue = 0) add soft delete criteria to the query | ||
* @method static removeSoftDelete() removes soft delete criteria from the criteriaQueue | ||
*/ | ||
class Criteria extends PhalconModel\Criteria | ||
{ | ||
private $modelCriterias = []; | ||
|
||
/** | ||
* removes soft deleted entries from the result. | ||
* | ||
* @param string $column | ||
* @param int $activeValue | ||
* | ||
* @return PhalconModel\Criteria | ||
* @internal param $add | ||
* | ||
*/ | ||
public function softDeleteCriteria($column = 'deleted', $activeValue = 0) | ||
{ | ||
return $this->andWhere($column.'='.$activeValue); | ||
} | ||
|
||
/** | ||
* alias to make more sense when calling it on query building. | ||
* | ||
* @return $this | ||
*/ | ||
public function withDeleted() | ||
{ | ||
return $this->removeSoftDelete(); | ||
} | ||
|
||
public function execute() | ||
{ | ||
$instance = $this; | ||
foreach ($this->modelCriterias as $criteria => $value) { | ||
$method = lcfirst($criteria).'Criteria'; | ||
$instance = $instance->$method(...$value); | ||
} | ||
|
||
return $instance->parentExecute(); | ||
} | ||
|
||
public function getPhql() | ||
{ | ||
return $this->createBuilder()->getPhql(); | ||
} | ||
|
||
public function getQuery() | ||
{ | ||
return $this->createBuilder()->getQuery(); | ||
} | ||
|
||
public function getActiveCriterias() | ||
{ | ||
return $this->modelCriterias; | ||
} | ||
|
||
private function parentExecute() | ||
{ | ||
return parent::execute(); | ||
} | ||
|
||
public function __call($name, $arguments) | ||
{ | ||
if (strpos($name, 'add') !== false) { | ||
$criteria = str_replace('add', '', $name); | ||
$this->modelCriterias[$criteria] = $arguments; | ||
} else if (strpos($name, 'remove') !== false) { | ||
$criteria = str_replace('remove', '', $name); | ||
if (isset($this->modelCriterias[$criteria])) { | ||
unset($this->modelCriterias[$criteria]); | ||
} | ||
} else { | ||
Throw new \Exception('Method '.$name.' does not exist.'); | ||
} | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#### Model Criteria usage example: | ||
|
||
```php | ||
class ModelCriteria extends Frogg\Criteria | ||
{ | ||
/** | ||
* @return \Phalcon\Mvc\Model\Criteria | ||
*/ | ||
public function ofUser($id) | ||
{ | ||
return $this->where('user_id = '.$id); | ||
} | ||
} | ||
``` | ||
|
||
```php | ||
$result = Model::query()->ofUser(1)->where('x' > 'y')->execute(); | ||
``` | ||
|
||
#### Global Criteria usage example: | ||
|
||
```php | ||
class Model extends Frogg\Model | ||
{ | ||
/** | ||
* @return ModelCriteria | ||
*/ | ||
public static function query(DiInterface $dependencyInjector = null) | ||
{ | ||
return parent::query($dependencyInjector)->addSoftDelete(); | ||
} | ||
} | ||
``` | ||
|
||
```php | ||
// this result applies softDelete for default, so all 'deleted = 1' results will be filtered | ||
// (this filter is added at the end of the query) | ||
$result = Model::query()->where('x' > 'y')->execute(); | ||
|
||
// but you can remove a global criteria calling removeCriteriaName | ||
$resultWithDeleted = Model::query()->removeSoftDelete()->where('x' > 'y')->execute(); | ||
// or for this specific method, we have an alias | ||
$resultWithDeleted = Model::query()->withDeleted()->where('x' > 'y')->execute(); | ||
``` | ||
|
||
#### Debugg tip | ||
|
||
You can get some infos like: | ||
```php | ||
$builder = Model::query()->where('x' > 'y'); | ||
$phql = $builder->getPhql(); | ||
$buildedQuery = $builder->getQuery(); | ||
$criterias = $builder->getActiveCriterias(); | ||
``` |