[英]How to transform the results of a stored procedure in to a list of object then convert to json in CI?
I am using CodeIgniter 2.2.6 + DataMapper, I have a question regarding how to transform the results of a stored procedure into DataMapper Models, then convert them to json. 我正在使用CodeIgniter 2.2.6 + DataMapper,我有一个关于如何将存储过程的结果转换为DataMapper模型,然后将它们转换为json的问题。
I have a model called Event: 我有一个名为Event的模型:
class Event extends DataMapper {
var $table = 'EVENT'; // map to EVENT table
}
with the following code, I can easily get the top 10 Event: 使用以下代码,我可以轻松获得前10个事件:
$event = new Event();
$event->get( 10, 0 );
$event->all_to_json(); //this convert the result to json following the table structure
Now I have a stored procedure getSpecialEvent(), the result of this SP has exactly same structure as Table EVENT, 现在我有一个存储过程getSpecialEvent(),此SP的结果与表EVENT的结构完全相同,
With the followng code, I do get the content but they are in Array format: 使用以下代码,我确实得到了内容,但它们是数组格式:
$sql = "call getSpecialEvent()";
$event = new Event();
$event = $this->db->query ($sql);
print_r ($event->result_array());
this will returned some thing like this: 这将返回如下内容:
Array
(
[0] => Array
(
[event_id] => 11
[title] => Test1
...
)
[1] => Array
(
[event_id] => 2
[title] => Test1
...
)
)
if I use this 如果我用这个
foreach ( $event as $obj ) {
print_r($obj);
}
I get empty array: 我得到空数组:
Array
(
)
Array
(
)
then I tried 然后我试了
print_r ($event->result());
it returns 它返回
Array
(
[0] => stdClass Object
(
[event_id] => 11
[title] => Test1
...
)
[1] => stdClass Object
(
[event_id] => 2
[title] => Test2
...
)
}
I used some code found on internet to cast stdClass Object to Event, it looks like ok, but when I call to_json() method, it doesn't work. 我使用互联网上找到的一些代码将stdClass对象转换为Event,看起来不错,但是当我调用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());
}
I printed he casted object, here it is: 我打印出他铸造的物体,这里是:
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
...
)
but $newObj->to_json() returns empty 但是$ newObj-> to_json()返回空
Array
(
)
Array
(
)
if I do a small test 如果我做一个小测试
$event = new Event ();
$event->event_id = 13;
$event->title = "xxxxx";
echo json_encode($event->to_json());
I do get: 我明白了:
{"event_id":13,"title":"xxxxx"....}
I don't know why the casted object doesn't work with to_json? 我不知道为什么casted对象不能与to_json一起使用?
这似乎是一个限制,强制转换的DataMapper对象(此处为Event)不被视为真正的DataMapper对象,然后我在Event中创建了一个方法,将所需的信息导出到另一个纯对象模型并使用json_encode(),此方法可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.