Php 返回模型对象数组,而不是CodeIgniter 3中的数组数组
我有一个CodeIgniter型号:Php 返回模型对象数组,而不是CodeIgniter 3中的数组数组,php,codeigniter,Php,Codeigniter,我有一个CodeIgniter型号: <?php class Person_model extends CI_Model { public function __construct() { $this->load->database(); } public function list_persons() { $query = $this->db->get('persons'); return
<?php
class Person_model extends CI_Model {
public function __construct() {
$this->load->database();
}
public function list_persons() {
$query = $this->db->get('persons');
return $query->result_array();
}
public function foobar() {
return 'Custom function here';
}
}
?>
是否可以让函数list_persons()
返回:
array(2) {
[0] => Person_model {
"id" => 1
"first_name" => "Test 1"
}
[1] => Person_model {
"id" => 2
"first_name" => "Test 2"
}
}
因此,我可以使用我在Person\u模型中编写的自定义函数,例如:
foreach($this->person_model->list_persons() as $person) {
echo $person->foobar();
}
提前感谢。像这样更新您的列表人员()
方法(使用$query->result()
):
美好的但是,返回的类是stdClass
而不是Person\u model
,无法调用foobar()
。有什么建议吗?我已经更新了答案。将表示模型类的字符串传递给$query->result()
,例如$query->result('Person\u model')
,以将其转换为模型类。效果非常好。谢谢这太棒了!我从来不知道这件事!谢谢@RendyEkoPrastiyo
foreach($this->person_model->list_persons() as $person) {
echo $person->foobar();
}
public function list_persons() {
$query = $this->db->get('persons');
return $query->result('Person_model');
}