Skip to content

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

Merged
merged 14 commits into from
Feb 4, 2020

Conversation

petr-buchin
Copy link
Contributor

Q A
Type feature/improvement
BC Break no
Fixed issues #2103

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.

@alcaeus
Copy link
Member

alcaeus commented Nov 11, 2019

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!

@petr-buchin
Copy link
Contributor Author

petr-buchin commented Nov 11, 2019

@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?

@alcaeus
Copy link
Member

alcaeus commented Nov 11, 2019

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.

@petr-buchin
Copy link
Contributor Author

petr-buchin commented Nov 24, 2019

@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!

Copy link
Member

@alcaeus alcaeus left a 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.

@petr-buchin
Copy link
Contributor Author

Hi @alcaeus, thanks for the review!

I think we also need to apply the same transformation to arrays
Do you mean we need to convert arrays the same way as we do with a single values?

If yes, how do you want to do this?
From what I see, there is no way to specify item type for a collection mapping (except reference collections), thus there is no way to know the type of an item in an array.

@petr-buchin
Copy link
Contributor Author

petr-buchin commented Dec 14, 2019

@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.
I fixed this behavior by retrieving id from target document (see changes in DocumentPersister::prepareQueryExpression() method).

Copy link
Member

@alcaeus alcaeus left a 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.

$customId2
);

// We need to have test cases inside this method, since we cannot access `$this->dm` in data provider
Copy link
Member

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);
    // ...
}

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'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],
Copy link
Member

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.

Copy link
Contributor Author

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

@petr-buchin
Copy link
Contributor Author

@alcaeus thanks for the suggestions!
I will fix this later today

@petr-buchin
Copy link
Contributor Author

@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 getReference() logic, data provider and closure is what you wanted.

Thanks!

@petr-buchin petr-buchin requested a review from alcaeus January 19, 2020 22:23
@alcaeus alcaeus self-assigned this Feb 4, 2020
@alcaeus
Copy link
Member

alcaeus commented Feb 4, 2020

Thanks @petr-buchin!

@alcaeus alcaeus merged commit ecb96f9 into doctrine:master Feb 4, 2020
@petr-buchin
Copy link
Contributor Author

@alcaeus thanks for review and for help with this!

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

Successfully merging this pull request may close these issues.

2 participants