<?php
use Doctrine\ORM\Query\Filter\SQLFilter;
use Doctrine\ORM\Mapping\ClassMetadata;
use PHPSnippets\Db\Entity\Test;
// create SQLFilter that works on every entity/table
// the filter works with SQL, not DQL
class FooFilter extends SQLFilter
{
public function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAlias)
{
return $targetTableAlias . '.name != \'test\'';
}
}
// add new filter to config and name it "foo"
$entityManager->getConfiguration()
->addFilter('foo', 'FooFilter');
$repo = $entityManager->getRepository(Test::CLASS);
// get unfiltered data
echo 'All data:' . PHP_EOL;
$tests = $repo->findAll();
foreach ($tests as $test) {
echo $test->id . ' ' .
$test->name . PHP_EOL;
}
// enable the filter named foo
$entityManager->getFilters()->enable('foo');
// get filtered data
echo PHP_EOL . 'Filtered data:' . PHP_EOL;
$tests = $repo->findAll();
foreach ($tests as $test) {
echo $test->id . ' ' .
$test->name . PHP_EOL;
}