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

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.
Reply


Messages In This Thread
RE: result object library for returning Query Builder query results - by mwhitney - 10-21-2015, 12:43 PM



Theme © iAndrew 2016 - Forum software by © MyBB