-
-
Notifications
You must be signed in to change notification settings - Fork 509
Added conversion from PHP values to DB values during query preparation #2104
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
Conversation
At a first glance this looks good, thanks! Can you please add some tests for this feature? I'd love to have this in 2.1! |
@alcaeus thanks for the review! Ok, I will add tests this week! Also, could you please take a look at that one test that is failing? |
Ignore that for now please - I just haven't found the time to introduce that test to the tip of my boot. It just needs a good kicking here and there. I'll try to fix it later today. |
@alcaeus I added two simple tests for this feature. If you want me to cover some other cases with tests, just tell me what cases I should consider. Thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the delay in reviewing this.
I think we also need to apply the same transformation to arrays. To make sure these cases are covered, I've suggested additional tests below.
tests/Doctrine/ODM/MongoDB/Tests/Functional/DocumentPersisterTest.php
Outdated
Show resolved
Hide resolved
tests/Doctrine/ODM/MongoDB/Tests/Functional/DocumentPersisterTest.php
Outdated
Show resolved
Hide resolved
Hi @alcaeus, thanks for the review!
If yes, how do you want to do this? |
@alcaeus I got your point: I updated tests as you requested and added exception for non-existent types. I also found and fixed the bug: at the moment ODM user can query by providing target document for reference field like that: ['referenceField' => $targetDocumentValue] but user cannot query by providing target document with a query operators, like that: ['referenceField' => ['$ne' => $targetDocumentValue]] For now ODM will try to convert target document value in query using target document's id type. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for not getting back to you sooner. From what I can tell, you understood what I meant with the array conversion. I have a few more suggestions below, but other than that this is looking good.
tests/Doctrine/ODM/MongoDB/Tests/Functional/DocumentPersisterTest.php
Outdated
Show resolved
Hide resolved
tests/Doctrine/ODM/MongoDB/Tests/Functional/DocumentPersisterTest.php
Outdated
Show resolved
Hide resolved
tests/Doctrine/ODM/MongoDB/Tests/Functional/DocumentPersisterTest.php
Outdated
Show resolved
Hide resolved
$customId2 | ||
); | ||
|
||
// We need to have test cases inside this method, since we cannot access `$this->dm` in data provider |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can work around this by using a closure for the getReference
logic above. Consider this short example:
public function dataProvider(): array
{
return [[function (DocumentManager $dm) { return $dm->getReference(...); }]];
}
/** @dataProvider dataProvider */
public function testCase(Closure $getReference)
{
$reference = $getReference($this->dm);
// ...
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've refactored this test using your advice, but I'm not sure that I understood you correctly.
Please look at the code and confirm if that's what you wanted.
The downside of closure is that we don't get $expected
and $query
as arguments of the test method as in the test above.
|
||
yield 'Direct comparison' => [ | ||
[ | ||
'query' => ['id' => $customId], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With the suggestion above, you can drop one array level:
yield 'Direct comparison' => [
'query' => ...,
'expected' => ...,
];
The key names for "query" and "expected" aren't necessary since PHPUnit passes the values of the array as arguments to the test. However, they make working with the data provider easier since you don't need to think which array is which and instead can use the parameter names.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, but only for the test method that does not use closure for getReference()
logic
@alcaeus thanks for the suggestions! |
@alcaeus sorry about not getting back with fixes sooner. I did all the fixes as you've requested, but I'm not sure about one single fix: please confirm if fix with Thanks! |
Thanks @petr-buchin! |
@alcaeus thanks for review and for help with this! |
Summary
This PR adds conversion of PHP values in queries to their DB representations using types functionality, and thus allows to query by php values rather that db values.