2012-11-05 53 views
3

我正在使用codeIgniter版本2.0.3。我想知道是否有一種簡單的方法來返回正確的數據類型的數據。例如,對於架構:CodeIgniter活動記錄返回Mysql數據類型而不僅僅是字符串

+-------+--------------+------+-----+---------+----------------+ 
| Field | Type   | Null | Key | Default | Extra   | 
+-------+--------------+------+-----+---------+----------------+ 
| id | int(11)  | NO | PRI | NULL | auto_increment | 
| term | varchar(255) | NO |  | NULL |    | 
| level | int(11)  | NO |  | NULL |    | 
+-------+--------------+------+-----+---------+----------------+ 

活動記錄result_array回報:

array(4) { 
    [0]=> 
    array(3) { 
    ["id"]=> 
    string(1) "1" 
    ["term"]=> 
    string(11) "Mesoamerica" 
    ["level"]=> 
    string(1) "1" 
    } 
    [1]=> 
    array(3) { 
    ["id"]=> 
    string(1) "2" 
    ["term"]=> 
    string(6) "Mexico" 
    ["level"]=> 
    string(1) "1" 
    } 
    [2]=> 
    array(3) { 
    ["id"]=> 
    string(1) "3" 
    ["term"]=> 
    string(10) "indigenous" 
    ["level"]=> 
    string(1) "2" 
    } 
    [3]=> 
    array(3) { 
    ["id"]=> 
    string(1) "4" 
    ["term"]=> 
    string(5) "ruins" 
    ["level"]=> 
    string(1) "2" 
    } 
} 

的想法是有類型來通過對結果集。 ID應該是INTEGER,而level應該是INTEGER。我通常只需要輸入我需要使用的數據。我沒有看到任何方法在文檔中將它推入codeIgniter,但希望有一個簡單的方法來做到這一點。我已經看到了在PDO中執行此操作的方法,但是codeIgniter爲我提供了一些方便操作嗎?

+0

那麼如果你使用'result()'而不是'result_array()'然後返回一個對象,那麼怎麼辦? – xbonez

+0

@xbonez @xbonez我只是想,我可能會錯過一些東西,它仍然返回所有字符串 – dm03514

+3

在這裏閱讀:http://codeigniter.com/forums/viewthread/110393/ –

回答

1

這裏是我做了一個幫手:

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 

/** 
* cast_fields 
* 
* Casts string values provided from database into 
* the appropriate datatype of possible. 
*/ 
if (! function_exists('cast_fields')) 
{ 
    function cast_fields($fields, $results) 
    { 
     for($i=0; $i<count($fields); $i++) { 
      $name = $fields[ $i ]->name; 
      $type = $fields[ $i ]->type; 
      $int_types = array(
       'int', 
       'tinyint', 
       'smallint', 
       'mediumint', 
       'bigint' 
      ); 
      if(in_array($type, $int_types)) { 
       for($j=0; $j<count($results); $j++) { 
        $casted = intval($results[ $j ]->{ $name }); 
        $results[ $j ]->{ $name } = $casted; 
       } 
      } 
     } 
     return $results; 
    } 

} 

/* End of file field_data_helper.php */ 
/* Location: ./application/helpers/field_data_helper.php */ 

到目前爲止,它僅適用於整數。您可以擴展它以支持其他數據類型。

如何使用它:

+0

這是一個很好的解決方案,直到你使用連接:( – RedactedProfile

0

我把這個不幸的做法:我只是改變了/system/database/DB_Result.php文件

Line 85的方法custom_result_object看起來是這樣的:

$object->$key = $value; 

變化它爲此:

​​

再加入此方法在任何地方:

/** 
* Scans a rowset to cast valid ints as integers instead of strings 
*/ 
private function recast($row) 
{ 
    foreach($row as $key => $value) 
    { 
     if(is_object($row)) { 
      if(is_numeric($value)) $row->{$key} = (int)$value; 
     } else { 
      if(is_numeric($value)) $row[$key] = (int)$value; 
     } 

    } 

    return $row; 
} 

然後在result_object()找到

$this->result_object[] = $row; 

和線之上,添加

$row = $this->recast($row); 

然後在result_array()找到

$this->result_array[] = $row; 

和線之上,添加

$row = $this->recast($row); 

然後在custom_row_object(),發現

$this->current_row = $n; 

和線之上,添加

$n = $this->recast($n); 

然後在row_object(),發現

$this->current_row = $n; 

和線之上,添加

$n = $this->recast($n); 

然後在row_array(),發現

$this->current_row = $n; 

和線之上,添加

$n = $this->recast($n); 

當然重鑄功能可以修改並更新爲您的解析需求,正確現在它只需要在沒有使用正則表達式的情況下投射整數。這種方法可以讓日期時間戳保持日期時間戳,但任何應該是數字值的東西都會被解析。 一些額外的花車等檢查也可以發生。

我只需要對API函數做到這一點,因爲json_encode鑄造了我所有的整數作爲硬字符串的id:"1",而不是整數id:1,需要一個解決方案,將整個網站反映。

相關問題