How to transform the results of a stored procedure in to a list of object then convert to json in CI?

337 Views Asked by At

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.

I have a model called Event:

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

with the following code, I can easily get the top 10 Event:

$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,

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.

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

    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?

1

There are 1 best solutions below

0
seaguest On

It seems to be a limitation, the casted DataMapper object (Event here) is not taken as a real DataMapper object, then I create a method in Event to export the needed info to aother pure object model and use json_encode(), this works.