CodeIgniter:通过 ActiveRecord 结果 ID 将 $subresult 添加到对象 $result
我在向 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你只需要:
编辑:
你只需要在 foreach 中分配引用(注意
$record
旁边的&
):I think you just need:
EDIT:
You just need to assign the reference in your foreach (note the
&
next to$record
):如果 rojoca 显示的方法不起作用,您可以这样做:
然后照常循环(除了
$record_id = $record['record_id']
而不是$record-> ;record_id
),并像这样引用您的行:尽管您可能不想如此依赖于始终与迭代索引完美匹配的 record_id 列。
If the method shown by rojoca doesn't work, you could do this:
Then loop through as usual (except do
$record_id = $record['record_id']
instead of$record->record_id
), and reference your row like this:Though you may not want to rely so heavily on that record_id column always matching perfectly with your iteration index.