bcosca / fatfree

A powerful yet easy-to-use PHP micro-framework designed to help you build dynamic and robust Web applications - fast!
2.66k stars 446 forks source link

cursor->load() doesn't reset cursor current position ($prt) #690

Closed skakz closed 9 years ago

skakz commented 10 years ago

Hi my code looks like this: (please I know that the following code can be done with a simple join, but this is not relevant in this case)

$table1= new DB\SQL\Mapper($db,TABLE1);
$table2= new DB\SQL\Mapper($db,TABLE2);

$table1->load('$condition');
while (!$table1->dry()) {
    $table2->load(array('COLUMN=?', $table1->COLUMN));
    while (!$table2->dry()) {
        // do something
        $table2->next();
    }
    $table1->next();
}

when $table2 is loaded for the SECOND time (with a different condition), $table2->dry() is true and the second while loop is never called. This is appening because cursor->prt (cursor current position) is not 0 after load(). I don't know if this is a desired behavior however if you reset the cursor before the load() it works fine. But I think that this should be automatic when you load a new record.

Thank you.

binarygeotech commented 10 years ago

I think you missed a step

'''''' $table2->reset() '''''' before '''''' $table2->load() ''''''

ikkez commented 10 years ago

usually this should work. cursor pointer is resetted right here: https://github.com/bcosca/fatfree/blob/master/lib/db/cursor.php#L150 $this->ptr=0

skakz commented 10 years ago

I saw that line of code, and this is why I think that resetting the cursor before invoking the load function shouldn't be necessary. However this doesn't work in my case. In debug mode I clearly see that $ptr is still different from zero after the load. Is there anything I can do for better investigating this problem?

bcosca commented 9 years ago

load() can retrieve more than one record. Your query needs to be reconstructed.

Since we do not have enough info to reproduce this issue and no follow-up comment was posted, please reopen if this issue persists in current version.