2016-11-18 17 views
1

我的模型有3种方法,从get_state_name()和get_district_name()返回的值是对象类型,我如何将它转换为字符串? 当我值绑定到形成一个地址,我得到这个错误将对象转换为codeiniter模型中的字符串

消息:类stdClass的客体不能转换成字符串

任何人可以帮助我吗?

get_job_address(); 

get_state_name(); 

get_district_name(); 

这里是我的代码

public function get_job_address($location_state_id,$district_name_id) { 
    $state=$this->get_state_name($location_state_id); 
    $district=$this->get_district_name($district_name_id); 
    return $district.', '.$state; 
} 
public function get_state_name($location_state_id) { 
    $this->db->select('country_name'); 
    $this->db->where('sid',$location_state_id); 
    $this->db->limit(1); 
    $query=$this->db->get('countries'); 
    return $query->row(); 
} 
public function get_district_name($district_name_id) { 
    $this->db->select('state_name'); 
    $this->db->where('sid',$location_name_id); 
    $this->db->limit(1); 
    $query=$this->db->get('states'); 
    return $query->row(); 
} 
+0

试试$ query-> result_array();或$ query-> row_array(); – Hemantwagh07

回答

0

回报,你想要的字段的值,如:

public function get_state_name($location_state_id) 
{ 
    $this->db->select('country_name'); 
    $this->db->where('sid',$location_state_id); 
    $this->db->limit(1); 
    $query=$this->db->get('countries'); 

    // check to make sure you got a result 
    if ($query->num_rows() == 1) 
    { 
     $row = $query->row(); 
     return $row->state_name ; 
    } 
    // if no result return false 
    else { return false; } 
} 

也回顾你的第三个方法,get_district_name(),在其中调用了错误的变量名。

2

问题就在这里:

$district=$this->get_district_name($district_name_id); 
return $district.', '.$state; 

$区

是性病类对象,你不能Concat的一个字符串对象。