hat*_*hua 4 php activerecord codeigniter object
我在向 Codeigniter 中的结果添加“子结果”时遇到问题。不确定如何添加到此对象。
$result->{$record_id}->threads = $threads;
Run Code Online (Sandbox Code Playgroud)
应该等于这样的
$result->1->threads = $threads;
Run Code Online (Sandbox Code Playgroud)
但我无法让它工作......我对 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;
}
?>
Run Code Online (Sandbox Code Playgroud)
我不想使用数组,在视图文件上使用这些数据更容易。
我认为你只需要:
$record->threads = $threads;
Run Code Online (Sandbox Code Playgroud)
编辑:
您只需要在 foreach 中分配引用(注意&
旁边的$record
):
foreach($records as &$record)
{
//...
$record->threads = $threads;
}
return $records;
Run Code Online (Sandbox Code Playgroud)