mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
Merge pull request #9098 from sminnee/fix-9097
FIX: Repeated iteration should return all records
This commit is contained in:
commit
d0b4f61310
@ -48,8 +48,11 @@ class MySQLQuery extends Query
|
||||
public function seek($row)
|
||||
{
|
||||
if (is_object($this->handle)) {
|
||||
// Fix for https://github.com/silverstripe/silverstripe-framework/issues/9097 without breaking the seek() API
|
||||
$this->handle->data_seek($row);
|
||||
return $this->nextRecord();
|
||||
$result = $this->nextRecord();
|
||||
$this->handle->data_seek($row);
|
||||
return $result;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
@ -105,8 +105,12 @@ class MySQLStatement extends Query
|
||||
public function seek($row)
|
||||
{
|
||||
$this->rowNum = $row - 1;
|
||||
|
||||
// Fix for https://github.com/silverstripe/silverstripe-framework/issues/9097 without breaking the seek() API
|
||||
$this->statement->data_seek($row);
|
||||
return $this->next();
|
||||
$result = $this->next();
|
||||
$this->statement->data_seek($row);
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function numRecords()
|
||||
@ -132,9 +136,4 @@ class MySQLStatement extends Query
|
||||
}
|
||||
return $row;
|
||||
}
|
||||
|
||||
public function rewind()
|
||||
{
|
||||
return $this->seek(0);
|
||||
}
|
||||
}
|
||||
|
@ -168,16 +168,15 @@ abstract class Query implements Iterator
|
||||
* Iterator function implementation. Rewind the iterator to the first item and return it.
|
||||
* Makes use of {@link seek()} and {@link numRecords()}, takes care of the plumbing.
|
||||
*
|
||||
* @return array
|
||||
* @return void
|
||||
*/
|
||||
public function rewind()
|
||||
{
|
||||
if ($this->queryHasBegun && $this->numRecords() > 0) {
|
||||
$this->queryHasBegun = false;
|
||||
$this->currentRecord = null;
|
||||
return $this->seek(0);
|
||||
$this->seek(0);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -272,4 +272,25 @@ class DatabaseTest extends SapphireTest
|
||||
$result = DB::query('SELECT TRUE')->first();
|
||||
$this->assertInternalType('int', reset($result));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that repeated iteration of a query returns all records.
|
||||
* See https://github.com/silverstripe/silverstripe-framework/issues/9097
|
||||
*/
|
||||
public function testRepeatedIteration()
|
||||
{
|
||||
$inputData = ['one', 'two', 'three', 'four'];
|
||||
|
||||
foreach ($inputData as $i => $text) {
|
||||
$x = new MyObject();
|
||||
$x->MyField = $text;
|
||||
$x->MyInt = $i;
|
||||
$x->write();
|
||||
}
|
||||
|
||||
$query = DB::query('SELECT "MyInt", "MyField" FROM "DatabaseTest_MyObject" ORDER BY "MyInt"');
|
||||
|
||||
$this->assertEquals($inputData, $query->map());
|
||||
$this->assertEquals($inputData, $query->map());
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user