Welcome Guest, Not a member yet? Register   Sign In
how can I create an empty database query object?
#1

[eluser]fritzthecat[/eluser]
Hi. I need to pass the results of a subroutine to a function that expects a query object, and on occasions I need to create an empty db query object - something like

Code:
function getStuff( $id=false ) {
    if( !$id )    {
        return $this->db->query();
    }    else    {
        return $this->db->get_where( 'stuff', "id=$id" );
}

how would you do it?

thanks
#2

[eluser]Thorpe Obazee[/eluser]
Code:
function getStuff( $id=false ) {
    if( !$id )    {
        return $this->db->query();
    }    else    {
// made some correction on the code
        return $this->db->get_where( 'stuff', array('id' => $id) );
}

I don't think I get the idea of returning of query object(I probably need to sleep.it's 1am here and I am hungry) but I'd return FALSE if I don't retrieve anything. and check from the function expecting it if it receives a boolean FALSE.
#3

[eluser]fritzthecat[/eluser]
I'd do the same, or return an array. It's not my idea.

The function calling the code I'm writing gets the result and tries to call results_array() on it without checking it. Hence, it must be of type query otherwise it'll fail.
#4

[eluser]NogDog[/eluser]
Not sure I entirely understand, but could you kludge it with something like this?
Code:
return $this->db->query("SELECT * FROM stuff WHERE 1=2"); // always returns 0 rows
#5

[eluser]Colin Williams[/eluser]
You should employ the rule of a single exit point:

Code:
function getStuff($id=false)
{
    $result = array();
    if ($id)    
    {
        $result = $this->db->get_where( 'stuff', array('id' => $id) );
    }
    return $result;
}

Then you can come in later and put what you need in $result before the conditional. My hunch is that the reason your want an "empty" db object is because you want to avoid errors where your controller tries to use $result->row(), etc. Just don't be lazy and run an isset() check first. Actually, I wouldn't even return db objects ever.

You could also just run the query.. if id is FALSE, you'll get an empty result. (No sense in running a dummy query as previously suggested)

Code:
function getStuff($id=false)
{
    return $this->db->get_where( 'stuff', array('id' => $id) );;
}
#6

[eluser]fritzthecat[/eluser]
Thanks Colin / NogDog. It's not a matter of me being lazy, but interacting with other people's code I cannot change.

My first approach would be to do just what you suggest Colin, but the query I am returning is actually a multiple join, and I don't want to run it if I don't have to, so I am been using something similar to NogDog's dummy query.

I just hoped there'd be a 'proper' way of doing it, just like in normal php you can just say return array() or something
#7

[eluser]xwero[/eluser]
The proper way IMO is as Colin suggests in his last snippet. If a where condition is false i don't see how there will be more processing involved in a join statement than in a single table statement? Do you have information about this or is it something that doesn't feel good?
#8

[eluser]fritzthecat[/eluser]
No, you are right, I have no info on this, I just know that joins use up more memory than single table queries and assumed that would be true even if there are no matches. Still, it feels somewhat strange not being able to create an empty query object.
#9

[eluser]jedd[/eluser]
[quote author="fritzthecat" date="1239293335"]Still, it feels somewhat strange not being able to create an empty query object.[/quote]

Well, you are attempting to work around an ugly hack by implementing an ugly hack of your own rather than trying to resolve the actual problem - bad extant and ostensibly immutable code.

In that context, it is less surprising that the language doesn't implicitly encourage this behaviour.

Are you really sure you can't slip a one-line isset or eval check for FALSE in the target function?
#10

[eluser]fritzthecat[/eluser]
No, the target is off limits. Never mind, it's a minor thing.




Theme © iAndrew 2016 - Forum software by © MyBB