What are Doctrine Filters? Check these few slides or see Usage to get the knowledge.
They are present in Doctrine by default. This package only simplifies their use in modular application.
$ composer require zenify/doctrine-filters
Register extension in config.neon
:
extensions:
- Zenify\DoctrineFilters\DI\FiltersExtension
- Symplify\SymfonyEventDispatcher\Adapter\Nette\DI\SymfonyEventDispatcherExtension
# Kdyby\Doctrine or another Doctrine to Nette implementation
Let's create our first filter, which will hide all deleted items on front. So called "soft delete" - data remains in database, but are filtered out in frontend.
First, we create class implementing Zenify\DoctrineFilters\Contract\FilterInterface.
use Doctrine\ORM\Mapping\ClassMetadata;
use Zenify\DoctrineFilters\Contract\FilterInterface;
final class SoftdeletableFilter implements FilterInterface
{
public function addFilterConstraint(ClassMetadata $entity, string $alias) : string
{
if ($entity->getReflectionClass()->hasProperty('isDeleted')) {
return "$alias.isDeleted = 0");
}
return '';
}
}
Then register as service:
# app/config/config.neon
services:
- SoftdeletableFilter
And that's it!
The management wants to show deleted content only to logged user with admin role.
To setup condition, we just implement Zenify\DoctrineFilters\Contract\ConditionalFilterInterface.
use Nette\Security\User;
use Zenify\DoctrineFilters\Contract\ConditionalFilterInterface;
final class SoftdeletableFilter implements ConditionalFilterInterface
{
/**
* @var User
*/
private $user;
public function __construct(User $user)
{
$this->user = $user;
}
public function addFilterConstraint(ClassMetadata $entity, string $alias) : string
{
// same as above
}
public function isEnabled() : bool
{
if ($this->user->isLoggedIn() && $this->user->hasRole('admin')) {
return FALSE;
}
return TRUE;
}
}
Voilá!
composer check-cs
vendor/bin/phpunit
Rules are simple:
- new feature needs tests
- all tests must pass
- 1 feature per PR
We would be happy to merge your feature then!