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
. No configuration is required.
Usage
The basic idea of this library is applying 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 name matching the name of the
filtered field: for example, in the code above, the filterd field username
expects
a db field u.username
.
If it's not the case, you can defined a static property $dbField
in your criterion class.