Skip to content

Instantly share code, notes, and snippets.

@guilhermeblanco
Created February 17, 2011 16:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guilhermeblanco/831995 to your computer and use it in GitHub Desktop.
Save guilhermeblanco/831995 to your computer and use it in GitHub Desktop.
EntityRepository::applyFilter(Filter $filter)
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);
}
}
@beberlei
Copy link

For userland code why not, but that doesnt help as a generic ways as its only for QueryBuilder and doesnt help with persister queries

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment