Welcome Guest, Not a member yet? Register   Sign In
result object library for returning Query Builder query results

(10-21-2015, 12:43 PM)mwhitney Wrote: When you load the library, if it has already been loaded previously, the existing instance of the library is returned, and the constructor is not executed again.

In both cases, you return $this->result, which is a property which has been assigned an instance of stdClass. Since this is a class, when you assign the result of $this->return_object->output($query) to a variable (or, in your case, an index in an array), you're creating a reference to the instance of $this->result in $this->return_object.

Honestly, I'm not 100% sure why you would get the first one twice, rather than the second one twice, unless the second one didn't return a result.

You should be able to fix this by using `clone` when returning $this->result from output(), or by defining $this->result as an array, then casting it to an object as needed. You may also want to reset $this->result in output(), since anything you don't set will still be set to the value from the previous call, so, currently:
- if you pass a falsey value for $data, everything in $this->result will be from the previous call
- if you pass a truthy object for $data, but num_rows() is less than or equal to 0 for some reason, $this->result->data and $this->result->count will be from the previous call

Also, note that last_query() doesn't work if the current database configuration has save_queries disabled.

Hello mwhitney,
Thanks for your answer. I thought, each time a model is loaded, a separate instance of return_object 'lives' in that model (so the constructor of this class is loaded for each instance)...
It seems I will have to enhance my CI-related OOP knowledge  Blush

Kind regards,


Messages In This Thread
RE: result object library for returning Query Builder query results - by Zeff - 10-22-2015, 12:17 AM

Theme © iAndrew 2016 - Forum software by © MyBB