toarray 学说不转换关系

发布于 2024-08-26 15:21:57 字数 401 浏览 7 评论 0原文

我按照教义文档开始。这是 文档

我的代码是

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

糖粟与秋泊 2024-09-02 15:21:57

通过使用 find 方法,您仅检索了用户数据,这就是为什么 toArray 的返回仅限于该数据的原因。您需要指定要加载的附加数据,执行此操作的最佳位置通常是在原始查询中。从您链接到的示例中,添加选择部分:

$q = Doctrine_Query::create()
    ->select('u.*, e.*, p.*')  // Example only, select what you need, not *
    ->from('User u')
    ->leftJoin('u.Email e')
    ->leftJoin('u.Phonenumbers p')
    ->where('u.id = ?', 1);

然后,当对其中的结果进行 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:

$q = Doctrine_Query::create()
    ->select('u.*, e.*, p.*')  // Example only, select what you need, not *
    ->from('User u')
    ->leftJoin('u.Email e')
    ->leftJoin('u.Phonenumbers p')
    ->where('u.id = ?', 1);

Then when toArray'ing the results from that, you should see the associated email and phonenumber data as well.

蓝戈者 2024-09-02 15:21:57

我还注意到一个异常现象,如果您先调用该关系然后调用 ToArray,该关系就会以某种方式被包含在内。我的意思是,采取你自己的例如,

$User = Doctrine_Core::getTable("User")->find(1); 
$num= $User->Phonenumbers->office; // assumed a field 'office' in your phone num table
$userArray = $user->toArray(true);

在上面的情况下, $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,

$User = Doctrine_Core::getTable("User")->find(1); 
$num= $User->Phonenumbers->office; // assumed a field 'office' in your phone num table
$userArray = $user->toArray(true);

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文