CodeIgniter:通过 ActiveRecord 结果 ID 将 $subresult 添加到对象 $result

发布于 2024-09-30 00:55:11 字数 1040 浏览 1 评论 0原文

我在向 Codeigniter 中的结果添加“子结果”时遇到问题。不确定如何添加到此对象。


$result->{$record_id}->threads = $threads;

应该等于这样的东西

$result->1->threads = $threads;

,但我无法让它工作......我对 OOP 并不陌生,但这是我第一次尝试这样做。

<?php

function get() {

 $this->db->select(array(

  'record_id',  'record_data',  'record_date',

 ));

 $this->db->from('records');

 $sql   = $this->db->get();
 $records  = $sql->result();

 foreach($records as $record){

  $record_id = $record->record_id;

  $this->db->select(array(

   'thread_id',   'thread_parent',   'thread_data',    'thread_date',

  ));

  $this->db->from('records_thread');
  $this->db->where(array(

   'thread_recordid'  => $record_id,

  ));

  $sql   = $this->db->get();
  $threads = $sql->result();

  # this is where i'm having issues \/

  $records->{$record_id}->threads = $threads;

 }

 return $records;

}
?>

我不想使用数组,在视图文件上使用这些数据更容易。

I'm having issues adding a "sub result" to a result in Codeigniter. Not sure how to add to this object.


$result->{$record_id}->threads = $threads;

should equal something like this

$result->1->threads = $threads;

but I can't get it to work... I'm not new to OOP but this is the first I've tried to do this.

<?php

function get() {

 $this->db->select(array(

  'record_id',  'record_data',  'record_date',

 ));

 $this->db->from('records');

 $sql   = $this->db->get();
 $records  = $sql->result();

 foreach($records as $record){

  $record_id = $record->record_id;

  $this->db->select(array(

   'thread_id',   'thread_parent',   'thread_data',    'thread_date',

  ));

  $this->db->from('records_thread');
  $this->db->where(array(

   'thread_recordid'  => $record_id,

  ));

  $sql   = $this->db->get();
  $threads = $sql->result();

  # this is where i'm having issues \/

  $records->{$record_id}->threads = $threads;

 }

 return $records;

}
?>

I don't want to use arrays and it's easier to use this data on the view file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

野生奥特曼 2024-10-07 00:55:11

我认为你只需要:

$record->threads = $threads;

编辑:

你只需要在 foreach 中分配引用(注意 $record 旁边的 & ):

foreach($records as &$record)
{
    //...
    $record->threads = $threads;
}

return $records;

I think you just need:

$record->threads = $threads;

EDIT:

You just need to assign the reference in your foreach (note the & next to $record):

foreach($records as &$record)
{
    //...
    $record->threads = $threads;
}

return $records;
青芜 2024-10-07 00:55:11

如果 rojoca 显示的方法不起作用,您可以这样做:

$records = $sql->result_array();

然后照常循环(除了 $record_id = $record['record_id'] 而不是 $record-> ;record_id),并像这样引用您的行:

$records[$record_id]['threads'] = $threads;

尽管您可能不想如此依赖于始终与迭代索引完美匹配的 record_id 列。

If the method shown by rojoca doesn't work, you could do this:

$records = $sql->result_array();

Then loop through as usual (except do $record_id = $record['record_id'] instead of $record->record_id), and reference your row like this:

$records[$record_id]['threads'] = $threads;

Though you may not want to rely so heavily on that record_id column always matching perfectly with your iteration index.

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