Doctrine 2 - Eager loading doesn't appear to work with ManyToMany

China☆狼群 提交于 2019-12-06 14:08:35

What are you trying to achieve? If you want to have a single query against your dB in which you fetch all the needed entities, you do need to write a custom query manually, it's not a matter of eager or lazy loading.

You could write something like this

$sequence_runs=$repository->createQueryBuilder('sr')->addSelect(['mts', 'fu'])
->leftJoin('sr.materialTypeString', 'mts')
->leftJoin('sr.users', 'fu')
->getQuery()->getResult();

You use eager loading when you want doctrine to do all the needed queries no matter if you use your associations in your code or not, while you use lazy loading to have your associations fully hydrated only if you access them in your code, but the number of queries will be the same in case you access all your associations in your code.

The point is: are you showing a paginated list of entities? Then, try to write your own query with DQL or a query builder, fetch loading all the necessary entities in reason of your views. Are you showing a single entity? Then fetch it from the repository lazy loading your associations and let doctrine do the job!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!