While working on a Symfony2 web application recently, I was tasked with mapping a complex data model to Doctrine 2 entities and checking that the design we chose had minimal impact on performance. Now, although I’ve used Doctrine 2 before, this was my first foray into doing something a little out of the ordinary (besides associating Blog to Comment, for example).
Along the way, I found some issues with filtering associations and this blog post will hopefully explain the problem and provide a couple of solutions if you’re experiencing a similar situation.
Firstly, here’s a simple chunk of the Entity Relationship Diagram we came up with and the area that was causing me a headache.
Talk to one of our digital experts
Tom Houdmont
Head of Business Solutions
Do you have an idea or a project you need support with?
Tom leads Box UK’s Business Solutions team and has over 15 years experience in the web industry. Tom is passionate about creating impactful solutions that solve real problems and deliver the outcomes our clients need.
I went ahead and added the annotations for the employee association to the department entity; I was then able to retrieve all the employees in that department by calling $department->getEmployees()
I then wanted to be able to get all the employees in a department that are of a particular employee type (full-time, part-time, etc.). Surely, I could do that in 5 minutes? I assumed I could just do something like this:
I did some digging to find the methods I needed but it seems that filtering my employee collection at the Doctrine level can’t be done (see here).
Doctrine Filter
So, I decided to go down the route of creating a method in a repository class which used QueryBuilder to fetch a department’s employees that are of a particular employee type:
This did the trick, but it didn’t feel very clean because I’d have to run another query which I felt was unnecessary, especially if I’d already eagerly loaded the employee and could retrieve them with $department->getEmployees().
Doctrine Criteria
Following this I decided to try a different route and bumped into the Criteria class. This allows you filter a collection not at the Doctrine level but on the returned collection we get when calling $department–>getEmployees(). It also has basic DQL methods which allow you to powerfully filter your collection using various conditions. Here’s what I came up with:
The eagle-eyed among you will have also noticed that the $criteria–>where() clause is filtering on the employeeType field of my employee entity even though I’ve supplied it with an employee id! It magically works out the join column for you, presumably by cleverly examining the JoinColumn annotation.
Both solutions have their pros and cons (performance for example) but I eventually chose the Criteria option. I suggest you try them out and, until we get a fix for the Doctrine association filter, choose the solution that best suits your needs.
Pete Withers-Jones
Head of Development
Pete Withers-Jones leads Box UK’s Development Practice, and has over 20 years of experience in software development, working across various industries and technologies.
Subscribe now and get our expert articles straight to your inbox!
"*" indicates required fields
Related Insights
Blog
Maximising the Effectiveness of Digital Strategies in Government Organisations