Welcome Guest, Not a member yet? Register   Sign In
"You must submit a valid result object" returned even though I'm passing an object.
#1

[eluser]Josh Kendall[/eluser]
Hi, I hope someone can offer some advice. I'm trying to use the DB Utility to export a result as a CSV file. Problem is, I need to update part of the result before export. After about an hour of converting the result_array() to an object (since I couldn't update the object) I'm now being told "You must submit a valid result object", even though I'm submitting an object that looks exactly like the object $query->result() produces.

Here is the results of printing a "true" result object:

Code:
Array
(
    [0] => stdClass Object
        (
            [type] => staff
            [lastname] => Member1
            [firstname] => Staff
            [email] => [email protected]
            [status] =>
            [training] => 4
        )

    [1] => stdClass Object
        (
            [type] => user
            [lastname] => User
            [firstname] => Basic
            [email] => [email protected]
            [status] => 0:0:0
            [training] => 4
        )

    [2] => stdClass Object
        (
            [type] => user
            [lastname] => User
            [firstname] => Another Basic
            [email] => [email protected]
            [status] => 0:0:0
            [training] => 4
        )

)

and here is my object after making the changes and converting it from a result array to an object:

Code:
Array
(
    [0] => stdClass Object
        (
            [type] => staff
            [lastname] => Member1
            [firstname] => Staff
            [email] => [email protected]
            [status] =>
            [training] => 4
        )

    [1] => stdClass Object
        (
            [type] => user
            [lastname] => User
            [firstname] => Basic
            [email] => [email protected]
            [status] => Elesson: 0, Page: 0
            [training] => 4
        )

    [2] => stdClass Object
        (
            [type] => user
            [lastname] => User
            [firstname] => Another Basic
            [email] => [email protected]
            [status] => Elesson: 0, Page: 0
            [training] => 4
        )

)

And here is the code I'm using in my controller:

Code:
// ------ EXPORT FUNCTIONS
        function export($file)
        {

            $data = explode('::', $file);
            $file = $data[1].'-'.time();
            $type = $data[0];
            $this->load->dbutil();
            if($type == 'user_progress'){
                $query = $this->db->query("SELECT * FROM `x_training_".$this->session->userdata('training')."_answers`");
            } else if($type == 'activity_export'){
                $query = $this->db->query("SELECT `type`, `lastname`, `firstname`, `email`, `status`, `training` FROM `x_users` where `type`!='admin' order by `training` ASC, `type` ASC");
            }
            $result = $query->result_array();
            $count = count($result);
            $i = 0;
            while($i < $count){
                if(ereg(':', $result[$i]['status'])){
                    $ex = explode(':', $result[$i]['status']);
                    $result[$i]['status'] = 'Elesson: '.$ex[1].', Page: '.$ex[2];
                }
                $i++;
            }

            $i = 0;
            $object = array();
            while($i < $count){
                $object[$i] = $this->arr2obj($result[$i]);
                $i++;
            }

            $csv = $this->dbutil->csv_from_result($object);
            $this->load->helper('download');
            if(force_download($file.'.xls', $csv)){
                redirect($_SERVER['HTTP_REFERER']);
            }
            
        }

and this is the function that converts the array to an object:

Code:
function arr2obj($arg_array) {
            $tmp = new stdClass; // start off a new (empty) object
            foreach ($arg_array as $key => $value) {
                if (is_array($value)) { // if its multi-dimentional, keep going :)
                    $tmp->$key = arr2obj($value);
                } else {
                    if (is_numeric($key)) { // can't do it with numbers :(
                        die("Cannot turn numeric arrays into objects!");
                    }
                    $tmp->$key = $value;
                }
            }
            return $tmp; // return the object!
        }

Any help would be appreciated.
#2

[eluser]wiredesignz[/eluser]
the csv_from_result utility requires functions named field_names(), list_fields() and result_array() within the true result object.

Why not just modify the data in the original result object and pass it on?
#3

[eluser]rfrancis[/eluser]
[quote author="wiredesignz" date="1202192090"]the csv_from_result utility requires functions named field_names(), list_fields() and result_array() within the true result object.

Why not just modify the data in the original result object and pass it on?[/quote]

I'm actually struggling with this very same problem. How do modify the original result object itself so I can pass it on?

Thanks in advance for any help.
#4

[eluser]Nathan Pitman (Nine Four)[/eluser]
Anyone ever find a solution to this, I've modified the original object but when I pass it to csv_from_result it resets to the original result object... :?
#5

[eluser]WanWizard[/eluser]
csv_from_output() internally calls result_array() method of the query result.

If the result has been fetched before, that result is returned (= the result_array property of the object), if not, it fetches the rows from the database driver (i.e. the original resultset).

So before you call csv_from_output(), fetch the result array in your model, and modify it. If you fetch it into a local variable, make sure to write that back to $query_object->result_array, so your modified values are exported to csv.
#6

[eluser]Nathan Pitman (Nine Four)[/eluser]
Thanks, got it working using the same method you describe but I needed to modify the column headers also so ended up rolling my own csv_from_result function which allowed for an additional parameter including column headings. Smile




Theme © iAndrew 2016 - Forum software by © MyBB