toarray 学说不转换关系
我按照教义文档开始。这是 文档。
我的代码是
$User = Doctrine_Core::getTable("User")->find(1);
当我通过 $User->Phonenumbers 访问关系时,它有效。当我使用 toArray() 方法将 User 对象转换为数组时,它不会将关系转换为数组。它只是显示 $User 数据。
我错过了什么吗?
I followed doctrine documnetation to get started. Here is the documentation.
My code is
$User = Doctrine_Core::getTable("User")->find(1);
when I access relations by $User->Phonenumbers, it works. When I convert User object to array by using toArray() method, it does not convert relations to array. It simply display $User data.
Am I missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通过使用 find 方法,您仅检索了用户数据,这就是为什么 toArray 的返回仅限于该数据的原因。您需要指定要加载的附加数据,执行此操作的最佳位置通常是在原始查询中。从您链接到的示例中,添加选择部分:
然后,当对其中的结果进行 toArray 时,您还应该看到关联的电子邮件和电话号码数据。
By using the find method you've only retrieved the User data which is why the return of toArray is limited to that data. You need to specify the additional data to load, and the best place to do this is usually in the original query. From the example you linked to, add the select portion:
Then when toArray'ing the results from that, you should see the associated email and phonenumber data as well.
我还注意到一个异常现象,如果您先调用该关系然后调用 ToArray,该关系就会以某种方式被包含在内。我的意思是,采取你自己的例如,
在上面的情况下, $userArray 以某种方式包含整个关系。如果我们删除 $num 分配,它就不会。
我猜测这是由于原则上只首先获取一条记录,并且只有当您尝试访问外键值时,它才会获取其他相关表
I also noticed an anomaly with this where if you call the relationship first then call the ToArray, the relationship somehow gets included. what i mean is that, taking your own eg,
In the above case, $userArray somehow contains the whole relationship. if we remove the $num assignment it doesn't.
am guessing this is due to doctrine only fetching the one record first, and it's only when you try to access foreign key values that it fetches the other related tables