2016-11-18 57 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的一個字符串對象。