Blog: Don't Do This At Home #6: Repeat The Same Filter For All Queries
Don't Do This At Home #6: Repeat The Same Filter For All Queries
Can you spot the problem in the following snippet?
The published()
filter appears in all other filters. Ok, that was easy, it's the title of the post! But how to do it better?
Why not create a specialized query class filtering on published artivcles by default? All the frontend controllers should use this FrontendArticleQuery
instead of the more generic ArticleQuery
. It would look like this:
This method can add more default filters (for instance, to restrict to articles linked to an author, articles arleady indexed by a search engine, etc.) Now, everytime you call FrontendArticleQuery::create()
, you know you only retreive published articles. No need to add this filter to other filters anymore.