2012-11-05 2 views
3

codeIgniter 버전 2.0.3을 사용하고 있습니다. 올바른 데이터 형식으로 데이터를 반환하는 간단한 방법이 있는지 궁금합니다.CodeIgniter Active Record 문자열 대신 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" 
    } 
} 

아이디어는 유형이 결과 집합을 뚫고 올 것입니다. IDINTEGER이어야하고 levelINTEGER이어야합니다. 나는 보통 내가 사용해야하는 데이터를 그냥 타입 변환한다. 나는 이것을 문서에서 codeIgniter에 적용 할 방법을 찾지 못했지만 이것을하기위한 쉬운 방법이 있기를 바라고 있습니다. PDO에서이 작업을 수행하는 방법을 보았지만 codeIgniter가이를 위해 일종의 편의를 제공합니까?

+0

'result_array()'대신'result()'를 사용하면 객체를 반환합니다. – xbonez

+0

@ 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

을 :

$object->$key = $value; 

변화 : 난 그냥 방법 custom_result_object에서

Line 85이처럼 보이는 /system/database/DB_Result.php 파일을 변경 그것 :

다음

/** 
* 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; 
} 

$this->result_object[] = $row; 

그 선 위에 찾을 result_object()result_array()

을 찾을

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

다음 추가 :

if(is_numeric($value)) $object->$key = (int)$value; 
else $object->$key = $value; 

는 어디서든이 방법을 추가

을 찾아
$this->result_array[] = $row; 

그 라인 위에,

$this->current_row = $n; 

그 선 위에 찾아 custom_row_object()에 다음

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

를 추가 row_object()

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

다음 추가

$this->current_row = $n; 

그 라인 위에,

$this->current_row = $n; 

그 선 위에 찾아 row_array()에 다음

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

를 추가

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

그리고 물론 참 -_- 기능을 추가 수정 및 귀하의 파싱 요구에 맞게 업데이트 할 수 있습니다. 이제는 정규 표현식을 사용하지 않아도되는 정수를 캐스팅합니다. 이 방식을 사용하면 날짜 시간 소인을 날짜 시간 소인 그대로 유지할 수 있지만 숫자 값으로 간주되는 모든 것은 구문 분석됩니다. 수레 등의 추가 검사가 수행 될 수도 있습니다.

json_encode가 모든 정수를 하드 문자열로 캐스팅했기 때문에 API 함수에이 작업을 수행하기 만하면됩니다. 정수 대신 정수 id:1이 필요하고 전체 사이트에 반영되는 솔루션이 필요했습니다.

관련 문제