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

Fix missing hint query option during find #2351

Merged
merged 3 commits into from
Aug 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/Doctrine/ODM/MongoDB/Query/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ private function runQuery()

switch ($this->query['type']) {
case self::TYPE_FIND:
$queryOptions = $this->getQueryOptions('select', 'sort', 'skip', 'limit', 'readPreference');
$queryOptions = $this->getQueryOptions('select', 'sort', 'skip', 'limit', 'readPreference', 'hint');
Copy link
Member

Choose a reason for hiding this comment

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

For now this fixes the bug, but I'm wondering if we should be more lenient with the options we accept. find take a lot more options than these few, and we may do well to allow people to utilise them. @jmikola any suggestions on this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can work out a list of options from the Builder and Collection classes and write a bunch of tests for those in a separate PR.

Copy link
Member

Choose a reason for hiding this comment

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

Let's hold off on what Jeremy says - not sure we need to test for every single one of them, but I'm rather thinking to just pass everything that we know isn't from ODM on to the driver.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

From what I can see, only maxTimeMS and snapshot are still missing. With snapshot being deprecated in the Find class, so I'm not sure if that should be added.

Copy link
Member

Choose a reason for hiding this comment

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

@TimoBakx: I'm not sure what documentation you're referring to, but there are many more options supported by the extension and library:

I think it makes sense for the builder to provide some API to add arbitrary options that may not be explicitly handled in ODM but are known to be supported in the extension/library. However, that would likely require some internal refactoring to the Query class, since many of these fields appear to be stuffed directly into the internal $query property. Handling this might require segregating options into $query['options'] so we no longer need to rely on getQueryOptions to extract specific keys.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jmikola I looked at all the options that have their own method in the query Builder class (and are added to the $this->query array there and checked which of those were used in the Find class code and were also missing in the $this->getQueryOptions call (which means that the instructions to the Builder would be ignored).

Moving things to separate options would indeed be better, but I'm not sure how much of a BC break that would be. It might be easier to add the missing values first. 🙂

$queryOptions = $this->renameQueryOptions($queryOptions, ['select' => 'projection']);

$cursor = $this->collection->find(
Expand Down
12 changes: 12 additions & 0 deletions tests/Doctrine/ODM/MongoDB/Tests/Query/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,18 @@ public function testFindQuery()
$this->assertEquals($expected, $qb->getQueryArray());
}

public function testFindWithHint(): void
{
$qb = $this->getTestQueryBuilder()
->find(User::class)
->hint('foo');

$expected = 'foo';

$this->assertEquals($expected, $qb->debug('hint'));
$this->assertEquals($expected, $qb->getQuery()->debug('hint'));
}

public function testUpsertUpdateQuery()
{
$qb = $this->getTestQueryBuilder()
Expand Down
24 changes: 24 additions & 0 deletions tests/Doctrine/ODM/MongoDB/Tests/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,30 @@ public function testCountOptionInheritance()
$this->assertSame(100, $query->execute());
}

public function testFindWithHint(): void
{
$cursor = $this->createMock(Traversable::class);

$collection = $this->getMockCollection();
$collection->expects($this->once())
->method('find')
->with(['foo' => 'bar'], ['hint' => 'foo'])
->will($this->returnValue($cursor));

// Using QueryBuilder->find adds hint to the query array
$queryArray = [
'type' => Query::TYPE_FIND,
'query' => ['foo' => 'bar'],
'hint' => 'foo',
];

$query = new Query($this->dm, new ClassMetadata(User::class), $collection, $queryArray, []);

/* Do not expect the same object returned by Collection::find(), since
* Query::makeIterator() wraps the return value with CachingIterator. */
$this->assertInstanceOf(Traversable::class, $query->execute());
}

public function testFindOptionInheritance()
{
$nearest = new ReadPreference('nearest');
Expand Down