Answer the question
In order to leave comments, you need to log in
Is there an analogue of Yii scopes in Symfony 2?
Hello. Please tell me if there is an analogue of Yii scopes in Symfony 2? Scopes are an excellent convenient solution, but Symfony did not find a normal query building tool that solves the same problems.
// Used by Doctrine
Answer the question
In order to leave comments, you need to log in
Well, let's start with the fact that Symfony has nothing to do with doctrine.
Next... what problems do scopes solve? Reducing code duplication when building queries using DbCriteria? Well, in Doctrine, in principle, there are no such problems, since there is no DbCriteria itself and everything is usually in one place. If you need convenient methods for selections, just make methods in the repository. This is a kind of best practice, to encapsulate all the logic for working with the database in some kind of service.
If we take default scopes (those that are default and are hung on each request), then filters will be analogous. doctrine-orm.readthedocs.org/en/latest/reference/f... You can turn them off, just like you can resetScope in Yii.
Why default? yes, because for other cases it’s not worth doing such things at all, but simply encapsulating all the logic for data sampling in the repository and in general everything related to storing entities in the database should be taken out there and registered as a service. But the cases when we want to exclude the selection of elements with disabled=0 in all queries or something like that filters are suitable. Although it is worth avoiding any global handlers at all, it is unnecessary to use events, etc.
You can just declare a base repository with some helpers for building queries and inherit your repositories from it, but usually the selections are so different that you will only hurt yourself.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question