3

我正在使用 codeIgniter 2.0.3 版。我想知道是否有一种简单的方法可以返回具有正确数据类型的数据。例如对于架构:

+-------+--------------+------+-----+---------+----------------+
| 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应该是INTEGERlevel应该是INTEGER。我通常只是对需要使用的数据进行类型转换。我在文档中没有看到任何将其推送到 codeIgniter 的方法,但我希望有一种简单的方法可以做到这一点。我已经看到了在 PDO 中执行此操作的方法,但 codeIgniter 是否为我提供了某种便利?

4

2 回答 2

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

到目前为止,它仅适用于整数。您可以对其进行扩展以支持其他数据类型。

如何使用它:

// Get rows from database.
$query = $this->db->get_where( $this->table, $criteria, $limit, $offset );
$results = $query->result();

// Cast data to the correct datatypes.
$fields = $this->db->field_data( $this->table );
$this->load->helper('field_data_helper');
$results = cast_fields($fields, $results);
于 2014-06-21T20:37:52.350 回答
0

我采取了这种不幸的方法:我只是更改了/system/database/DB_Result.php文件

Line 85in 方法custom_result_object如下所示:

$object->$key = $value; 

将其更改为:

if(is_numeric($value)) $object->$key = (int)$value;
else $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);

当然,recast 函数可以根据您的解析需求进行修改和更新,现在它只是在需要的地方转换整数,而不使用正则表达式。这种方法让日期时间戳保持不变,但是任何应该是数值的东西都会被解析。还可以对浮点数等进行一些额外的检查。

我只需要为 API 函数执行此操作,因为 json_encode 将我所有的整数转换为硬字符串,id:"1"而不是整数id:1,并且需要一个能够反映整个站点的解决方案。

于 2014-11-19T06:14:30.920 回答