Download the PHP package garak/orm-criteria without Composer
On this page you can find all versions of the php package garak/orm-criteria. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package orm-criteria
ORM Doctrine Criteria
This library is meant to ease the filtering with Doctrine repositories. It's an ideal companion for PUGX FilterBundle.
Setup
Run composer require garak/orm-criteria
.
If you want to leverage the profiler info, you need to add the bundle to your kernel. You won't need to enable the bundle in other environments, its features will work anyway.
Usage
The basic idea of this library is to apply the Open/Closed principle (the "O" in SOLID), to avoid being forced to change your code every time you need to apply a new filter.
[!TIP] for the concept of "filter", refer to the FilterBundle mentioned above
You have two classes available: AbstractCriterion
and Filterer
.
Inject the latter into your repositories, then you can start creating
your criteria.
Let's suppose you have a UserRepository
, and you want to filter your users by
the following fields: "username", "enabled" (yes/no), and "country".
Let's create the following classes:
Then configure the services:
Now, let's use it in your repository:
Advanced Usage
You can pass your sorting options along wit the filters, like in this example:
$filters['_sort']['field'] = 'username';
$filters['_sort']['direction'] = 'DESC';
If your criterion needs something more sophisticated than the basic operators,
you can define a filter
method and add your logic. Example:
You can use different kinds of comparison operators, see the constants defined in
the Filterer
class.
By default, the library expects to find a database field matching the name of the
filtered field: for example, in the code above, the filtered field username
expects
a db field u.username
.
If it's not the case, you can define a static property $dbField
in your criterion class.