Reputation: 115
I have a project that i'm currently migrating from pdo/sql to doctrine, but that's not based in any framework (shame).
I need to add many filters to the products entity, so the rows matched by these filters will never output anywhere in the frontend. The filters are for example: products out of stock, products with empty names, no pictures, no description, column "disabled" set to 1, etc...
How could i create this filter? Is there any example?
I dont think it would be a good solution to use findOneBy and add a multiple-index array with all conditions i need in every page. Also sometimes i update these filters and its not cool to update it everywhere the product entity is used in the project.
Upvotes: 2
Views: 204
Reputation: 52513
Doctrine Filters:
You can i.e. use Doctrine Filters to filter entities with deletedAt property not null ( softdeletable ):
$config = new Doctrine\ORM\Configuration;
// ... your configuration
$config->addFilter('soft-deleteable', 'Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter');
Please have look at the source of the SoftDeletableFilter to understand how it works.
More information can be found in the Filters section of the documentation.
Furthermore:
A very easy implementation of filterable repositories can be achieved with KnpLabs/DoctrineBehaviors.
Please have a look at filterable.
Uses traits so php >= 5.4 is needed!
Upvotes: 1