Search code examples
phpoopsymfonydoctrinedql

When to use DQL vs Methods and Loops


When is it best to write a Doctrine query and when is it best to write methods for the object? This is what I perceive, but I'd like to get a more experienced opinion. I am trying to abide by good practice whenever possible.

Doctrine Query:

Pros:

  • fewer queries

Cons:

  • Can't be written as a method in an object (not reusable)

Writing a Method:

Pros:

  • resusable

Cons:

  • More Queries (lots of for loops)

Solution

  • I sometimes wonder the same thing, however given your example I would add a method to your teachers custom repository. This way there is no cons, and it is also reusable

    In other cases to help me decide I generally look at how many objects there potentially could be and how many sql statements doctrine is going to execute to do my lookup, if it is going to be a lot I will usually use DQL