Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query each method does not recognize if there are no more rows #10023

Closed
aliechti opened this issue Oct 27, 2015 · 7 comments
Closed

Query each method does not recognize if there are no more rows #10023

aliechti opened this issue Oct 27, 2015 · 7 comments

Comments

@aliechti
Copy link
Contributor

Like mentioned in: #9962 there is a problem with the recognition of the end of a resultset.

Example:
There is a table with 10 entries

foreach ($query->each() as $key => $value) {
    echo $key."\n";
}

after it selects the 10th row it throws an exception:

Exception 'PDOException' with message 'SQLSTATE[IMSSP]:
There are no more rows in the active result set.
Since this result set is not scrollable, no more data may be retrieved.'

in vendor\yiisoft\yii2\db\DataReader.php:111

Stack trace:
#0 vendor\yiisoft\yii2\db\DataReader.php(111): PDOStatement->fetch()
#1 vendor\yiisoft\yii2\db\BatchQueryResult.php(143): yii\db\DataReader->read()
#2 vendor\yiisoft\yii2\db\BatchQueryResult.php(112): yii\db\BatchQueryResult->fetchData()
#3 commands\TestController.php(26): yii\db\BatchQueryResult->next()
@SilverFire
Copy link
Member

As for me, it is not a feature - it's a bug, and it affects only MSSQL

@samdark samdark added the type:bug Bug label Oct 27, 2015
@samdark
Copy link
Member

samdark commented Oct 27, 2015

@SilverFire feature:db means it's database related, not the fact that it's a feature :)

@jadrovski
Copy link

yea it's a bug, how to fix? :-(

@samdark
Copy link
Member

samdark commented Dec 14, 2015

@alexkart
Copy link
Contributor

alexkart commented Jul 11, 2019

So, this can't be fixed in the framework code?
If so, there is a workaround:

$allRows = [];
try {
    foreach ($query->each() as $index => $row) {
        $allRows[$index] = $row;
    }
} catch (\PDOException $e) {
    if ($e->getMessage() !== 'SQLSTATE[IMSSP]: There are no more rows in the active result set.  Since this result set is not scrollable, no more data may be retrieved.') {
        throw $e;
    }
}

And it works correctly, $allRows will contain all rows. The same is true for batch().

@samdark
Copy link
Member

samdark commented Jul 11, 2019

So, this can't be fixed in the framework code?

Can't we add the above workaroud to each?

alexkart added a commit to alexkart/yii2 that referenced this issue Jul 15, 2019
alexkart added a commit to alexkart/yii2 that referenced this issue Jul 15, 2019
@alexkart
Copy link
Contributor

alexkart commented Jul 15, 2019

So, this can't be fixed in the framework code?

Can't we add the above workaroud to each?

Yes, I've created a PR

@samdark samdark added this to the 2.0.23 milestone Jul 15, 2019
alexkart added a commit to alexkart/yii2 that referenced this issue Jul 15, 2019
@samdark samdark closed this as completed Jul 15, 2019
samdark added a commit that referenced this issue Jul 15, 2019
* Closes #10023. Fix MSSQL "There are no more rows" exception when using `each()` and `batch()`

* #10023. Fix codeclimate issue

* Added since tag
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants