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

[Cursor] the current method does not return anything! #1183

Closed
ziaratban opened this issue Nov 29, 2020 · 5 comments · Fixed by #1186
Closed

[Cursor] the current method does not return anything! #1183

ziaratban opened this issue Nov 29, 2020 · 5 comments · Fixed by #1186
Assignees
Labels
needs-followup tracked-in-jira Ticket filed in Mongo's Jira system

Comments

@ziaratban
Copy link

After the last fetch, the current method of MongoDB\Driver\Cursor must be return null but not returns anything.(according to the doc).

Q A
OS CentOS 8
PHP 7.4.12
MongoDB extension version 1.9.0
MongoDB server MongoDB Enterprise 4.4.1
MongoDB environment Replica Set

Test.php

<?php
$mng = new \MongoDB\Driver\Manager('mongodb://user:pass@127.0.0.1:27017/myDB');
$mng->selectServer($mng->getReadPreference());
$cursor = $mng->executeQuery('myDB.myCollection',new \MongoDB\Driver\Query([],['batchSize' => 100]));
$cursor->SetTypeMap([
    'root' => 'array',
    'document' => 'array'
]);
var_dump($cursor->isDead());
var_dump($cursor->getId());
$cursor->rewind();
while(true){
    $row = $cursor->current();
    var_dump($row);
    $cursor->next();
}
root# php Test.php

output

bool(false)
object(MongoDB\Driver\CursorId)#4 (1) {
  ["id"]=>
  int(0)
}
array(13) {
...
}
array(13) {
...
}
PHP Notice:  Undefined variable: row in Test.php on line 14
NULL
PHP Fatal error:  Uncaught MongoDB\Driver\Exception\RuntimeException: Cannot advance a completed or failed cursor. in Test.php:15
Stack trace:
#0 Test.php(15): MongoDB\Driver\Cursor->next()
#1 {main}
  thrown in Test.php on line 15
@alcaeus alcaeus self-assigned this Nov 29, 2020
@alcaeus
Copy link
Member

alcaeus commented Nov 29, 2020

Hi @ziaratban! Thanks for reporting this. Are you running the latest version (1.9.0)? That version introduced changes to cursor iteration by implementing the Iterator interface, so that might have something to do with this. I'll investigate this and report back.

@ziaratban
Copy link
Author

Hi @alcaeus.
thanks for your reply.
yes. i am certain.

image

@jmikola
Copy link
Member

jmikola commented Dec 2, 2020

@alcaeus I believe this behavior may be expected given the implementation of Cursor::current(). return_value is only set if data is non-null, so perhaps an earlier guard statement with RETURN_NULL() is needed.

That said, @ziaratban should only call current() if valid() returns true, per PHP's own contract for the Iterator interface.

@agolin95 agolin95 added the tracked-in-jira Ticket filed in Mongo's Jira system label Dec 2, 2020
@agolin95
Copy link
Contributor

agolin95 commented Dec 2, 2020

@alcaeus
Copy link
Member

alcaeus commented Dec 22, 2020

Will be fixed in the upcoming patch release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-followup tracked-in-jira Ticket filed in Mongo's Jira system
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants