|
| 1 | +# VisibilityFilterBundle – A centralised approach to visibility of Doctrine Entities |
| 2 | + |
| 3 | +This bundle provides a Doctrine Filter which handles visibility filtering for Entities transparently for a whole |
| 4 | +application, removing the need to repeatedly phrase the filtering in every repository method of an Entity. Most notably, |
| 5 | +the filtering also applies to Doctrine queries that bypass the repository, like relationships declared in the entity |
| 6 | +mapping. |
| 7 | + |
| 8 | +## Getting started |
| 9 | + |
| 10 | +First, you need to declare this bundle as a composer dependency. |
| 11 | + |
| 12 | +```shell |
| 13 | +composer require webfactory/visibility-filter-bundle |
| 14 | +``` |
| 15 | + |
| 16 | +Next, the bundle needs to be registered to Symfony. Depending on your Symfony version, this might look like that: |
| 17 | + |
| 18 | +```php |
| 19 | +# src/bundles.php |
| 20 | + |
| 21 | +return [ |
| 22 | + # ... |
| 23 | + Webfactory\VisibilityFilterBundle\VisibilityFilterBundle::class => ['all' => true], |
| 24 | + # ... |
| 25 | +]; |
| 26 | +``` |
| 27 | + |
| 28 | +The filter class needs to be registered manually. |
| 29 | + |
| 30 | +```yaml |
| 31 | +# src/config.yml |
| 32 | +doctrine: |
| 33 | + orm: |
| 34 | + filters: |
| 35 | + visibility: Webfactory\VisibilityFilterBundle\Filter\VisibilityColumnConsideringSQLFilter |
| 36 | +``` |
| 37 | +
|
| 38 | +Important: The YAML key of the filter needs to `visibility`, otherwise the filter won't be activated on requests. |
| 39 | + |
| 40 | +## Configuring the visibility column |
| 41 | + |
| 42 | +This bundle assumes that the visibility determination is going to be based on a specific field in the Entity containing |
| 43 | +visibility information; e.g. functioning as a "visibility switch" containing "yes" or "no" or containing a visibility |
| 44 | +grade on a scale, based on which the visibility of the object will be determined. |
| 45 | + |
| 46 | +**Currently, only entities that have a visibility column configured will be filtered at all.** |
| 47 | + |
| 48 | +All you need to configure on your entity is *which* of its fields will be the one with the visibility information. |
| 49 | +You can do that by Adding the `VisibilityColumn()` annotation to that field. |
| 50 | + |
| 51 | +```php |
| 52 | +
|
| 53 | +use Doctrine\ORM\Mapping as ORM; |
| 54 | +use Webfactory\VisibilityFilterBundle\Annotation\VisibilityColumn; |
| 55 | +
|
| 56 | +/** |
| 57 | + * @ORM\Entity() |
| 58 | + */ |
| 59 | +class EntityWithVisibilityColumn |
| 60 | +{ |
| 61 | + // ... |
| 62 | + /** |
| 63 | + * @VisibilityColumn() |
| 64 | + * |
| 65 | + * @ORM\Column(type="string") |
| 66 | + * |
| 67 | + * @var string |
| 68 | + */ |
| 69 | + private $visibilityColumn; |
| 70 | + // ... |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +Please note that configuring more than one field as visibility column will throw an exception. |
| 75 | + |
| 76 | +## Replacing the filter strategy |
| 77 | + |
| 78 | +By default, the library makes you application only query entities from the database that have the string `y` in their |
| 79 | +visibility column. You can change this behaviour by overwriting the service |
| 80 | +`Webfactory\VisibilityFilterBundle\Filter\FilterStrategy` with your own implementation. |
| 81 | + |
| 82 | +Your implementation needs to implement the `FilterStrategy` interface. If you only want to change the `y` string to |
| 83 | +something different, you can use the `Webfactory\VisibilityFilterBundle\Filter\Strategy\ValueInField` implementation |
| 84 | +and provide it with a different `visibleValue` in its constructor. |
0 commit comments