In Symfony, how to get QueryBuilder from default findBy method?

Viewed 444

In Symfony 5, using Doctrine, how can I get QueryBuilder object (instead of results) from default entity repository methods like findBy, findOneBy, findAll?

I need QueryBuilder for:

  1. Passing it to KnpPaginator (requires specifically QueryBuilder instead of results)
  2. Possibly extending it with additional query logic in the future

I could just write a simple query (like $em->createQuery("SELECT a FROM Article a")), but I want to have access to filtering and ordering provided by default findBy method. I think writing my own QueryBuilder with filtering/sorting by any property would be a lot of work and I'm not sure I could implement it well even if I tried.

EDIT: even though it does not answer my question exactly, I have found a solution for my 1st use case (using KnpPaginator without writing custom queries): Custom data repository pagination.
This method allows to attach pagination to any query without changing it instead of writing a new one through QueryBuilder.

0 Answers
Related