Created
February 17, 2011 16:07
-
-
Save guilhermeblanco/831995 to your computer and use it in GitHub Desktop.
EntityRepository::applyFilter(Filter $filter)
This file contains hidden or 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
class EntityRepository | |
{ | |
public function applyFilter(Filter $filter) | |
{ | |
$qb = $this->createQueryBuilder('e'); | |
$filter->apply($qb); | |
return $qb->getQuery()->getResult(); | |
} | |
} | |
interface Filter | |
{ | |
public function apply(QueryBuilder $qb); | |
} | |
class FilterChain implements Filter | |
{ | |
// This could be a SplPriorityQueue too | |
private $filters = array(); | |
public function add(Filter $filter) | |
{ | |
$this->filters[] = $filter; | |
} | |
public function apply(QueryBuilder $qb) | |
{ | |
foreach ($this->filters as $filter) { | |
$filter->apply($qb); | |
} | |
} | |
} | |
namespace Filters\Group; | |
class GroupNameFilter implements Filter | |
{ | |
private $name; | |
public function __construct($name) | |
{ | |
$this->name = $name; | |
} | |
public function apply(QueryBuilder $qb) | |
{ | |
$qb->addSelect('g') | |
->innerJoin($qb->getRootAlias() . '.Group', 'g', Expr\Join::WITH, 'g.name = :groupName') | |
->setParameter('groupName', $this->name); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For userland code why not, but that doesnt help as a generic ways as its only for QueryBuilder and doesnt help with persister queries