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

Add new aggregation pipeline stages #1654

Merged
merged 10 commits into from
Oct 22, 2017

Conversation

alcaeus
Copy link
Member

@alcaeus alcaeus commented Oct 12, 2017

Fixes #1618.

This pull request adds support for the new aggregation pipeline stages added in MongoDB 3.4 by supporting them with mapping information. It also fixes field name translation in $lookup stages.

Todos:

  • Add documentation for new pipeline stages including examples
  • Update $lookup limitations about reference types; document support for storeAs=ref

@alcaeus alcaeus added this to the 1.2 milestone Oct 12, 2017
@alcaeus alcaeus self-assigned this Oct 12, 2017
@alcaeus alcaeus requested review from jmikola and malarzm October 12, 2017 14:58
@alcaeus alcaeus force-pushed the add-new-aggregation-stuff branch from 158c009 to 65945b3 Compare October 13, 2017 04:52
@@ -0,0 +1,49 @@
<?php

Copy link
Member

Choose a reason for hiding this comment

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

Missing license header

@@ -0,0 +1,49 @@
<?php

Copy link
Member

Choose a reason for hiding this comment

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

Missing license header

public function connectFromField($connectFromField)
{
// No targetClass mapping - simply use field name as is
if (!$this->targetClass) {
Copy link
Member

Choose a reason for hiding this comment

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

Missing spaces around !

}

// connectFromField doesn't have to be a reference - in this case, just convert the field name
if (!$this->targetClass->hasReference($connectFromField)) {
Copy link
Member

Choose a reason for hiding this comment

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

Missing spaces around !

}

$mappedByMapping = $this->targetClass->getFieldMapping($referenceMapping['mappedBy']);
switch ($mappedByMapping['storeAs']) {
Copy link
Member

Choose a reason for hiding this comment

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

These two switches are almost the same, what do you think about rewriting whole if-else block to:

$mapping = $referenceMapping['isOwningSide'] ? $referenceMapping : $this->targetClass->getFieldMapping($referenceMapping['mappedBy']);
// if for repository method
// whole switch

throw MappingException::repositoryMethodLookupNotAllowed($this->class->name, $connectFromField);
}

$mappedByMapping = $this->targetClass->getFieldMapping($referenceMapping['mappedBy']);
Copy link
Member

Choose a reason for hiding this comment

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

Should there be an exception for when mappedBy is not present? Or is that mappedBy or repositoryMethod?

Copy link
Member Author

Choose a reason for hiding this comment

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

There's an exception if repositoryMethod is present - I don't specifically check for mappedBy here. It'd probably be good from a defensive coding point of view. 👍

parent::from($this->targetClass->getCollection());

if ($referenceMapping['isOwningSide']) {
switch ($referenceMapping['storeAs']) {
Copy link
Member

Choose a reason for hiding this comment

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

Same switch again, maybe a private method accepting array $mapping would be better?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done - not sure if I want to extract it into a trait for use in Stage\Lookup as well :|

return array_map([$this, 'convertTargetFieldName'], $fieldName);
}

if (!$this->targetClass) {
Copy link
Member

Choose a reason for hiding this comment

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

Missing spaces around !


public static function connectFromFieldMustReferenceSameDocument($fieldName)
{
return new self("Cannot use field '$fieldName' as connectFromField in \$graphLookup stage. Reference must target the document itself.");
Copy link
Member

Choose a reason for hiding this comment

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

if I'm not mistaken, you missed an "a": "in a $graphLookup stage"

@alcaeus alcaeus force-pushed the add-new-aggregation-stuff branch from df4f013 to 2e74384 Compare October 16, 2017 15:15
@alcaeus alcaeus force-pushed the add-new-aggregation-stuff branch from 10f60b3 to bc70b7a Compare October 22, 2017 17:57
@alcaeus alcaeus changed the title Add new aggregation stuff Add new aggregation pipeline stages Oct 22, 2017
@alcaeus alcaeus merged commit 87af93d into doctrine:master Oct 22, 2017
@alcaeus alcaeus deleted the add-new-aggregation-stuff branch October 22, 2017 18:27
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.

Aggregation builder: Add support for aggregation pipeline stages added in MongoDB 3.4
2 participants