I am currently in the process of porting a recent symfony 1.4 application to Symfony2 and Doctrine2. The first thing I noticed once I converted the mapping information (we use YAML files), is that querying for user objects got into an infinite loop.
I played around with the associations and found that I had a cyclical one. I removed that one for debugging purposes, wondering why it would cycle through all those associations at all. I had just asked for all users.
Today I came across the same problem again with another object, only that it stopped after 930 queries and didn’t feature an endless loop. It seems that Doctrine2, when it populates the object, queries all objects for each association. No proxying, no lazy loading.
I enabled fetch: LAZY on that assocation and voilà, the auto-population stopped!
But now I want to have that as the default behavior, as the model has over 50 associations and I don’t want to have to add this line to all of them.
How do I activate lazy loading as the default in Doctrine 2?
After much sweat and reducing the model to a minimal example, I found out why Doctrine fetches all these multiple rows.
The culprit was the ORM tool I am using. When it imported the existing database structure, every relationship was deemed a “one-to-one” relationship. Changing the relationship to “one-to-many” on all 85 associations (as would be correct)
should dodid the trick.