webfactory / visibility-filter-bundle
Symfony Bundle that filters out invisible Doctrine entities in a centralised way
Installs: 1 357
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 4
Forks: 1
Open Issues: 9
Requires
- php: ^7.2|^8.1
- doctrine/annotations: ^1.12
- doctrine/doctrine-bundle: ^1.12.13|^2.0
- doctrine/orm: ^2.7.2
- symfony/config: ^5.4|^6.4
- symfony/dependency-injection: ^5.4|^6.4
- symfony/event-dispatcher: ^5.4|^6.4
- symfony/http-kernel: ^5.4|^6.4
Requires (Dev)
- dama/doctrine-test-bundle: ^6.5
- phpunit/phpunit: ^8.5
- symfony/framework-bundle: ^5.4|^6.4
- symfony/yaml: ^5.4|^6.4
- dev-master
- 1.4.0
- 1.3.0
- 1.2.0
- 1.1.1
- 1.1
- 1.0
- dev-php-cs-fixer/refs/heads/master
- dev-php-cs-fixer/refs/pull/39/merge
- dev-php-cs-fixer/refs/pull/37/merge
- dev-php-cs-fixer/refs/pull/35/merge
- dev-php-cs-fixer/refs/pull/31/merge
- dev-php-cs-fixer/refs/pull/33/merge
- dev-php-cs-fixer/refs/pull/29/merge
- dev-php-cs-fixer/refs/pull/27/merge
- dev-php-cs-fixer/refs/pull/25/merge
- dev-php-cs-fixer/refs/pull/22/merge
- dev-142739_Visibility-Filter-Bundle-Vern-nftige-L-sung-f-r-Parameter-Caching-Problem
- dev-php-cs-fixer-autocommit
This package is auto-updated.
Last update: 2024-10-27 08:50:44 UTC
README
This bundle provides a Doctrine Filter which handles visibility filtering for Entities transparently for a whole application, removing the need to repeatedly phrase the filtering in every repository method of an Entity. Most notably, the filtering also applies to Doctrine queries that bypass the repository, like relationships declared in the entity mapping.
Getting started
First, you need to declare this bundle as a composer dependency.
composer require webfactory/visibility-filter-bundle
Next, the bundle needs to be registered to Symfony. Depending on your Symfony version, this might look like that:
# src/bundles.php return [ # ... Webfactory\VisibilityFilterBundle\VisibilityFilterBundle::class => ['all' => true], # ... ];
The filter class needs to be registered manually.
# src/config.yml doctrine: orm: filters: visibility: Webfactory\VisibilityFilterBundle\Filter\VisibilityColumnConsideringSQLFilter
Important: The YAML key of the filter needs to visibility
, otherwise the filter won't be activated on requests.
Configuring the visibility column
This bundle assumes that the visibility determination is going to be based on a specific field in the Entity containing visibility information; e.g. functioning as a "visibility switch" containing "yes" or "no" or containing a visibility grade on a scale, based on which the visibility of the object will be determined.
Currently, only entities that have a visibility column configured will be filtered at all.
All you need to configure on your entity is which of its fields will be the one with the visibility information.
You can do that by Adding the VisibilityColumn()
annotation to that field.
use Doctrine\ORM\Mapping as ORM; use Webfactory\VisibilityFilterBundle\Annotation\VisibilityColumn; /** * @ORM\Entity() */ class EntityWithVisibilityColumn { // ... /** * @VisibilityColumn() * * @ORM\Column(type="string") * * @var string */ private $visibilityColumn; // ... }
Please note that configuring more than one field as visibility column will throw an exception.
Replacing the filter strategy
By default, the library makes you application only query entities from the database that have the string y
in their
visibility column. You can change this behaviour by overwriting the service
Webfactory\VisibilityFilterBundle\Filter\FilterStrategy
with your own implementation.
Your implementation needs to implement the FilterStrategy
interface. If you only want to change the y
string to
something different, you can use the Webfactory\VisibilityFilterBundle\Filter\Strategy\ValueInField
implementation
and provide it with a different visibleValue
in its constructor.