zenify/doctrine-filters

This package is abandoned and no longer maintained. The author suggests using the nettrine/orm package instead.

Doctrine Filters integration into Nette

v4.5.0 2020-10-15 11:06 UTC

README

Build Status Quality Score Code Coverage Downloads Latest stable

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.

Install

$ 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

Usage

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!

Limit Access by Role

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á!

Testing

composer check-cs
vendor/bin/phpunit

Contributing

Rules are simple:

  • new feature needs tests
  • all tests must pass
  • 1 feature per PR

We would be happy to merge your feature then!