控制器不调用模型功能

时间:2014-03-17 22:59:56

标签: php function codeigniter

我试图调用函数get_owner($id)来获取用户名,但它一直说:遇到PHP错误

Severity: Notice

Message: Trying to get property of non-object

Filename: incident/detail_page_admin.php

Line Number: 54

查看:

<p id ="detail_owner">Criador do Incidente: <?php echo $incident_own->name;?></p>

控制器:

$aux = $this->incident_model->count_incident($this->session->userdata('usuario_id'));
$avatar = $this->usuario_model->get_avatar($this->session->userdata('usuario_id'));
$incident_owner = $this->usuario_model->get_owner($id);
$this->load->view('template/cabecalho');
$b = $this->incident_model->get_incident_by_id($id);
$this->load->view('incident/detail_page_admin', array('incident_own' => $incident_owner, 'count_incident' => $aux, 'incident_show_by_id' => $b, 'avatar_array' => $avatar));
$this->load->view('template/rodape');

型号:

function get_owner ($id){
    $a = $this->db->query('select name from usuario where id = ?', array($id));
    return $a->result();
}

1 个答案:

答案 0 :(得分:0)

您希望返回$a->result()而不是在模型函数中返回$a->row(),然后它应该可以正常工作。

function get_owner ($id){
    $a = $this->db->query('select name from usuario where id = ?', array($id));
    return $a->row();
}

但是返回$a->row()只会返回一行,如果你只想要一行就可以了。但是,如果您希望返回多行,则可以执行

function get_owner ($id){
    $a = $this->db->query('select name from usuario where id = ?', array($id));
    return $a;
}

然后你想循环遍历视图中的结果行,如

<?php foreach ($incident_own->result() as $row)
{ ?>
    <p id ="detail_owner">Criador do Incidente: <?php echo $row->name;?></p>
<?php } ?>