【问题标题】:How to transform the results of a stored procedure in to a list of object then convert to json in CI?如何将存储过程的结果转换为对象列表,然后在 CI 中转换为 json?
【发布时间】:2016-03-22 15:55:03
【问题描述】:

我正在使用 CodeIgniter 2.2.6 + DataMapper,我有一个关于如何将存储过程的结果转换为 DataMapper 模型,然后将它们转换为 json 的问题。

我有一个名为 Event 的模型:

class Event extends DataMapper {
    var $table = 'EVENT';   // map to EVENT table
}

通过以下代码,我可以轻松获取前 10 名事件:

$event = new Event();       
$event->get( 10, 0 );
$event->all_to_json(); //this convert the result to json following the table structure

现在我有一个存储过程getSpecialEvent(),这个SP的结果和Table EVENT的结构完全一样,

使用以下代码,我确实得到了内容,但它们是数组格式:

$sql = "call getSpecialEvent()";
$event = new Event();
$event = $this->db->query ($sql);
print_r ($event->result_array());

这将返回类似这样的内容:

Array
(
    [0] => Array
        (
            [event_id] => 11
            [title] => Test1
            ...
        )

    [1] => Array
        (
            [event_id] => 2
            [title] => Test1
            ...
        )

)

如果我用这个

foreach ( $event as $obj ) {
    print_r($obj);
}

我得到一个空数组:

Array
(
)
Array
(
)

然后我尝试了

print_r ($event->result());

返回

Array
(
    [0] => stdClass Object
        (
            [event_id] => 11
            [title] => Test1            
            ...
        )

    [1] => stdClass Object
        (
            [event_id] => 2
            [title] => Test2           
            ...
        )
}

我使用网上找到的一些代码将 stdClass 对象转换为事件,看起来还可以,但是当我调用 to_json() 方法时,它不起作用。

function objectToObject($instance, $className) {
    return unserialize(sprintf(
            'O:%d:"%s"%s',
            strlen($className),
            $className,
            strstr(strstr(serialize($instance), '"'), ':')
            ));
}


foreach ( $event->result() as $obj ) {
    $newObj = $this->objectToObject($obj, "Event");
    print_r ($newObj);
    print_r ($newObj->to_json());
}

我打印了他铸造的物体,这里是:

    Event Object
    (
        [table] => EVENT
        [error] => 
        [stored] => 
        [model] => 
        [primary_key] => id
        [valid] => 
        [cascade_delete] => 1
        [fields] => Array
            (
            )

        [all] => Array
            (
            )

        [parent] => Array
            (
            )

        [validation] => Array
            (
            )

        [has_many] => Array
            (
            )

        [has_one] => Array
            (
            )

        [production_cache] => 
        [free_result_threshold] => 100
        [default_order_by] => 
        [_validated:protected] => 
        [_force_validation:protected] => 
        [_instantiations:protected] => 
        [_field_tracking:protected] => 
        [_query_related:protected] => Array
            (
            )

        [_include_join_fields:protected] => 
        [_force_save_as_new:protected] => 
        [_where_group_started:protected] => 
        [_group_count:protected] => 0

        [event_id] => 11
        [title] => test11
        ...
    )

但是 $newObj->to_json() 返回空

    Array
    (
    )
    Array
    (
    )

如果我做一个小测试

$event = new Event ();      
$event->event_id = 13;
$event->title = "xxxxx";
echo json_encode($event->to_json());

我明白了:

{"event_id":13,"title":"xxxxx"....}

我不知道为什么铸造的对象不能与 to_json 一起使用?

【问题讨论】:

  • 你试过这个 print_r ($event->result());
  • @MasoodRehman,请看我的更新,我不能把结果放在这里。

标签: php codeigniter codeigniter-datamapper


【解决方案1】:

这似乎是一个限制,铸造的DataMapper对象(此处为Event)不被视为真正的DataMapper对象,然后我在Event中创建一个方法将所需的信息导出到另一个纯对象模型并使用json_encode(),这行得通。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 2018-08-08
    • 1970-01-01
    • 1970-01-01
    • 2016-10-06
    相关资源
    最近更新 更多