Skip to content

Conversation

vanyabrovary
Copy link
Contributor

Q A
Is bugfix? ✔️
New feature?
Breaks BC?
Fixed issues (#388)

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a bug arising from passing an unexpected argument to the MongoDB driver's getId() method in several files.

  • Removed the extraneous argument from the getId() call in Cursor.php
  • Updated getId() calls in Query.php and BatchQueryResult.php to match the required method signature

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
src/file/Cursor.php Fixed the call to getId() by removing the wrongly passed argument
src/Query.php Corrected the getId() usage in logging to conform to the API
src/BatchQueryResult.php Adjusted the getId() call in batch data fetching to avoid argument error

public function getId()
{
return $this->getInnerIterator()->getId(true);
return $this->getInnerIterator()->getId();
Copy link

Copilot AI Jun 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the unsupported argument from getId(), which now aligns with the MongoDB driver's expected method signature.

Suggested change
return $this->getInnerIterator()->getId();
$innerIterator = $this->getInnerIterator();
if ($innerIterator instanceof \MongoDB\Driver\Cursor) {
return $innerIterator->getId();
}
throw new \RuntimeException('Inner iterator is not an instance of \MongoDB\Driver\Cursor.');

Copilot uses AI. Check for mistakes.

protected function fetchRows($cursor, $all = true, $indexBy = null)
{
$token = 'fetch cursor id = ' . $cursor->getId(true);
$token = 'fetch cursor id = ' . $cursor->getId();
Copy link

Copilot AI Jun 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated getId() by removing the extraneous argument to prevent runtime exceptions in logging.

Suggested change
$token = 'fetch cursor id = ' . $cursor->getId();
$token = 'fetch cursor hash = ' . spl_object_hash($cursor);

Copilot uses AI. Check for mistakes.

Comment on lines +132 to 133
$token = 'fetch cursor id = ' . $cursor->getId();
Yii::info($token, __METHOD__);
Copy link

Copilot AI Jun 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the invalid argument from the getId() call to fix the bug related to incorrect method usage.

Suggested change
$token = 'fetch cursor id = ' . $cursor->getId();
Yii::info($token, __METHOD__);
if (method_exists($cursor, 'getId')) {
$token = 'fetch cursor id = ' . $cursor->getId();
Yii::info($token, __METHOD__);
} else {
Yii::warning('Cursor does not support getId()', __METHOD__);
}

Copilot uses AI. Check for mistakes.

@samdark
Copy link
Member

samdark commented Jun 27, 2025

Please add a line for CHANGELOG. We'll fix test pipelines to run it.

@vanyabrovary
Copy link
Contributor Author

@samdark Added line to CHANGELOG #391

@leoshtika
Copy link

leoshtika commented Jul 4, 2025

Hi, I have the same issue.
I'm currently using the MongoDB PHP extension v2.1.1, and after updating to yiisoft/yii2-mongodb v3.0.3, I'm getting the following error:
ArgumentCountError: MongoDB\Driver\Cursor::getId() expects exactly 0 arguments, 1 given

In the previous version (v3.0.2), there was a try/catch block to handle the difference in method signature between MongoDB extension versions (1.x vs 2.x). That fallback has been removed in 3.0.3, but it's still necessary for compatibility with the 2.x extension, where getId() no longer accepts any arguments.

To support both extension versions, I suggest restoring the compatibility logic in the fetchRows() in yiisoft/yii2-mongodb/src/Query.php

try {
    $token = 'fetch cursor id = ' . $cursor->getId(true);
} catch (\ArgumentCountError $e) {
    $token = 'fetch cursor id = ' . $cursor->getId();
}

Let me know if you'd like me to submit a patch.
Thanks again!

@samdark samdark merged commit 8f53e48 into yiisoft:master Jul 11, 2025
6 of 7 checks passed
@samdark
Copy link
Member

samdark commented Jul 11, 2025

Thank you!

@HagnMarkus
Copy link

In the previous version (v3.0.2), there was a try/catch block to handle the difference in method signature between MongoDB extension versions (1.x vs 2.x). That fallback has been removed in 3.0.3, but it's still necessary for compatibility with the 2.x extension, where getId() no longer accepts any arguments.

To support both extension versions, I suggest restoring the compatibility logic in the fetchRows() in yiisoft/yii2-mongodb/src/Query.php

Or increase the minimum version of the MongoDB PHP extension to 2.0.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants