如何防止学说懒惰加载一对一的关系? [英] How to prevent Doctrine from lazy loading one to one relationsip?

查看:87
本文介绍了如何防止学说懒惰加载一对一的关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:如果您遇到类似的问题,请本主题对你有兴趣

If you're having similar issues This Topic will be of interest to you

我拥有一对一双向关系的User和UserSettings。看来,即使我没有在我的页面中使用任何UserSettings值,原则懒惰也会加载它。

I have User and UserSettings with one to one bidirectional relationship. It appears that even if i do not use any UserSettings values in my page, doctrine lazy loads it anyways.

这是预期的行为吗?为什么Doctrine获取这些数据,即使我没有在我的页面中使用它?如果我无法阻止它,我每次检索用户对象时都必须将UserSettings加入用户,但这是不必要的。

Is this expected behavior? Why is Doctrine fetching this data even though I'm not using it in my page? If I'm unable to stop it, I would have to join this UserSettings to User every time I retrieve user object, but this is so unnecessary.

我可以做些什么来防止这种情况发生?

What can I do to prevent this from happening?

加载数据的代码:

->createQuery('SELECT p, u, s FROM TestPostBundle:Post p LEFT JOIN p.user u LEFT JOIN p.sub s WHERE p.id IN (:ids)')
->setParameter('ids', $ids)
->getResult();

在twig我循环通过帖子和显示Post数据和关联的用户,但我从来没有要求任何UserSettings变量,我根本没有访问它们。

The in twig I loop through posts and display Post data and associated user, but I never request any UserSettings variables, I'm not accessing them at all.

推荐答案

我也遇到同样的问题。看来,从反面学说的查询也查询拥有方面。请参阅此讨论

I also faced same problem. It seems that when querying from inverse side doctrine also queries the owning side. See this discussion.

这篇关于如何防止学说懒惰加载一对一的关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆