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 support for custom types with requireSQLConversion and ResultSetMappingBuilder::generateSelectClause() #9326

Merged
merged 1 commit into from
Jan 9, 2022

Conversation

kimhemsoe
Copy link
Member

@kimhemsoe kimhemsoe commented Jan 4, 2022

No description provided.

@derrabus
Copy link
Member

derrabus commented Jan 4, 2022

Can you elaborate what this change does?

@kimhemsoe
Copy link
Member Author

kimhemsoe commented Jan 4, 2022

When using ResultSetMappingBuilder::generateSelectClause() with custom types with Type::canRequireSQLConversion() => true, we would not call Type::convertToPHPValueSQL() to inject the required sql.

I am trying to fix that here when using ResultSetMappingBuilder::*EntityFromClassMetadata() methods. ResultSetMappingBuilder can help with some of the pain of building native queries.

There is still the case when using ResultSetMappingBuilder::addFieldResult() and etc which is not covered. i am affraid that changing those could maybe break peoples hacks out there.

Not sure if i can/should fix thee psalm issues.

Also could be nice if we could do this one place one day. I stole some of this from

protected function getSelectColumnSQL($field, ClassMetadata $class, $alias = 'r')

PS. Sorry for jumping the gun with review request with failing tests.

@kimhemsoe
Copy link
Member Author

Added test using addFieldResult(), works fine and i was wrong that *EntityFromClassMetadata() is a speciel case.

@kimhemsoe kimhemsoe changed the title Add support for custom types with requireSQLConversion when using Res… Add support for custom types with requireSQLConversion and ResultSetMappingBuilder::generateSelectClause() Jan 5, 2022
@kimhemsoe
Copy link
Member Author

@derrabus How do i solve the psalm issue?

@greg0ire
Copy link
Member

greg0ire commented Jan 8, 2022

@kimhemsoe the field you are accessing is documented as possibly unset:

in #8409 , but looking at the codebase, there are several places where it is assumed to exist, and then the resulting Psalm/Phpstan errors are ignored in the baseline.

I think that after this piece of code is executed, it's always set:

protected function validateAndCompleteFieldMapping(array $mapping): array
{
// Check mandatory fields
if (! isset($mapping['fieldName']) || ! $mapping['fieldName']) {
throw MappingException::missingFieldName($this->name);
}
if ($this->isTypedProperty($mapping['fieldName'])) {
$mapping = $this->validateAndCompleteTypedFieldMapping($mapping);
}
if (! isset($mapping['type'])) {
// Default to string
$mapping['type'] = 'string';
}
// Complete fieldName and columnName mapping
if (! isset($mapping['columnName'])) {
$mapping['columnName'] = $this->namingStrategy->propertyToColumnName($mapping['fieldName'], $this->name);
}

@greg0ire
Copy link
Member

greg0ire commented Jan 8, 2022

@orklah , do you remember why you marked that key as possibly unset?

beberlei
beberlei previously approved these changes Jan 8, 2022
Copy link
Member

@beberlei beberlei left a comment

Choose a reason for hiding this comment

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

PR is good! This makes sense to add

You should update the baseline of psalm with these two errors included as @greg0ire said its done in other places already, we can then independently look into making it an always existing column in our psalm mapping.

You can update the baseline with ./vendor/bin/psalm --set-baseline=psalm-baseline.xml

@@ -16,6 +17,7 @@
use function sprintf;
use function strpos;
use function strtolower;
use function var_dump;
Copy link
Member

Choose a reason for hiding this comment

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

I believe this is unused, not sure why phpcs/psalm don't complain

Copy link
Member

Choose a reason for hiding this comment

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

That would be because of this:

orm/phpcs.xml.dist

Lines 34 to 35 in 760397c

<!-- quite overkill to ignore this, but its necessary since Attributes are not detected yet -->
<exclude name="SlevomatCodingStandard.Namespaces.UnusedUses.UnusedUse" />

Copy link
Member

Choose a reason for hiding this comment

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

Ah forgot about having to add that myself :)

Copy link
Member

Choose a reason for hiding this comment

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

Fixed in #9338

@orklah
Copy link
Contributor

orklah commented Jan 8, 2022

@orklah , do you remember why you marked that key as possibly unset?

Probably a lack of knowledge of Doctrine. What I can see is a property initialized to empty array and not initialized in constructor. But maybe Doctrine make sure to initialize some properties before anything happens with the property

@greg0ire
Copy link
Member

greg0ire commented Jan 8, 2022

What I can see is a property initialized to empty array and not initialized in constructor.

That's fieldMappings with an s, which contains an array of FieldMapping, which is true even if it's an empty array.

I think I'll just try removing that ? and see how SA reacts.

$class = $this->em->getClassMetadata($this->declaringClasses[$columnName]);
$fieldName = $this->fieldMappings[$columnName];
$classFieldMapping = $class->fieldMappings[$fieldName];
$columnSql = $tableAlias . '.' . $classFieldMapping['columnName'];
Copy link
Member Author

Choose a reason for hiding this comment

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

@beberlei What we if i add a FieldMapping class with array access in another PR? How much will break? Or is there another better plan to get rid of some of the many arrays?

Copy link
Member

Choose a reason for hiding this comment

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

that is a 3.0 task

@greg0ire
Copy link
Member

greg0ire commented Jan 8, 2022

Addressed in #9339

@derrabus
Copy link
Member

derrabus commented Jan 8, 2022

This does not look like a bugfix. Let's target 2.11.x instead?

@kimhemsoe kimhemsoe changed the base branch from 2.10.x to 2.11.x January 9, 2022 08:55
@kimhemsoe kimhemsoe added this to the 2.11.0 milestone Jan 9, 2022
@kimhemsoe
Copy link
Member Author

"Use var_dump()" removed, rebased and retarget to 2.11.x. psalm issues should be solved been when 2.10.x is merged up to 2.11.x

@greg0ire greg0ire closed this Jan 9, 2022
@greg0ire greg0ire reopened this Jan 9, 2022
@greg0ire greg0ire merged commit c6d8aec into doctrine:2.11.x Jan 9, 2022
@greg0ire
Copy link
Member

greg0ire commented Jan 9, 2022

Thanks @kimhemsoe !

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

Successfully merging this pull request may close these issues.

5 participants