S
S
symnoob2021-12-06 21:30:42
symfony
symnoob, 2021-12-06 21:30:42

How to filter all queries by user?

I have a lot of listings in my project, is there any event where I could just slip in the filtering conditions?
something like

if ($entity instanceof OwnerInterface) {
            //some Logik
}

It’s just that it’s just somehow pointless to shove in each controller ->findByUser($user), and there is some more logic.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2021-12-06
@symnoob

Why is it pointless? Not pointless. Controllers can be different: API, Console. Web… there can be different filtering everywhere. So it's definitely not pointless.
As for logic, it's correct. It should not be in the controller! To do this, create your own filtering service. Pass the Query Builder into it and filter as needed and where needed.
You can read more on the EasyAdmin example https://symfony.com/bundles/EasyAdminBundle/curren... or on Laravel example for understanding https://habr.com/en/post/485520/
Laravel example just for understanding how it can be done.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question