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

Feature/many to many support #509

Merged
merged 13 commits into from
Oct 8, 2022

Conversation

pietaj
Copy link
Contributor

@pietaj pietaj commented Aug 18, 2022

I am targeting this branch, because the bundle does not support m:n auditing and the subject was already mentioned
here #302, and here #321 but was never finished

Changelog

### Added
- Support for ManyToMany

@VincentLanglet
Copy link
Member

VincentLanglet commented Aug 18, 2022

I fixed the cs in #510.
Please rebase and revert the extra style changes.

We don't work on this library a lot so the more the PR will be small, the easier it will be for us to review ^^

And take a look at the build ;)

@VincentLanglet VincentLanglet requested a review from a team August 18, 2022 12:35
@pietaj pietaj force-pushed the feature/many-to-many-support branch from 5d79c19 to ce94043 Compare August 22, 2022 13:37
@pietaj
Copy link
Contributor Author

pietaj commented Aug 22, 2022

I've rebased the code and reverted the style changes.

I've also left the deprecation fixes out of this PR as it makes the code harder to review.

@pietaj
Copy link
Contributor Author

pietaj commented Aug 24, 2022

@VincentLanglet I've checked the stan / psalm issues reported by the CI and I can fix them, no problem, but this will make the code changes in this PR even more complex, as there are quite a lot of things to review already. Shall I proceed with the fixes and throw them all in a separate commit? What would be the most convenient way to proceed?

Comment on lines 1002 to 1003
$tableName = $this->config->getTablePrefix(
).$assoc['joinTable']['name'].$this->config->getTableSuffix();
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
$tableName = $this->config->getTablePrefix(
).$assoc['joinTable']['name'].$this->config->getTableSuffix();
$tableName = $this->config->getTablePrefix()
.$assoc['joinTable']['name']
.$this->config->getTableSuffix();

Comment on lines 1008 to 1012
$query = 'SELECT '.implode(
', ',
$columnList
).' FROM '.$tableName.' e WHERE '.$whereSQL.' ORDER BY e.'.$this->config->getRevisionFieldName(
).' DESC';
Copy link
Member

Choose a reason for hiding this comment

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

Prefer using sprintf

Comment on lines 1068 to 1069
$tableName = $this->config->getTablePrefix(
).$targetAssoc['joinTable']['name'].$this->config->getTableSuffix();
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
$tableName = $this->config->getTablePrefix(
).$targetAssoc['joinTable']['name'].$this->config->getTableSuffix();
$tableName = $this->config->getTablePrefix()
.$targetAssoc['joinTable']['name']
.$this->config->getTableSuffix();

Comment on lines 1074 to 1078
$query = 'SELECT '.implode(
', ',
$columnList
).' FROM '.$tableName.' e WHERE '.$whereSQL.' ORDER BY e.'.$this->config->getRevisionFieldName(
).' DESC';
Copy link
Member

Choose a reason for hiding this comment

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

prefer sprintf

* @return string
*
* @psalm-param ClassMetadata<object> $targetClass
* @psalm-param array{cache?: array, cascade: array<string>, declared?: class-string, fetch: mixed, fieldName: string, id?: bool, inherited?: class-string, indexBy?: string, inversedBy: null|string, isCascadeRemove: bool, isCascadePersist: bool, isCascadeRefresh: bool, isCascadeMerge: bool, isCascadeDetach: bool, isOnDeleteCascade?: bool, isOwningSide: true, joinColumns?: array<array{name: string, referencedColumnName: string, unique?: bool, quoted?: bool, fieldName?: string, onDelete?: string, columnDefinition?: string, nullable?: bool}>, joinColumnFieldNames?: array<string, string>, joinTable?: array, joinTableColumns?: list<mixed>, mappedBy: null|string, orderBy?: array, originalClass?: class-string, originalField?: string, orphanRemoval?: bool, relationToSourceKeyColumns?: array, relationToTargetKeyColumns?: array, sourceEntity: class-string, sourceToTargetKeyColumns?: array<string, string>, targetEntity: class-string, targetToSourceKeyColumns?: array<string, string>, type: int, unique?: bool} $assoc
Copy link
Member

Choose a reason for hiding this comment

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

You can use @phpstan-import-type AssociationMapping from ... https://github.com/doctrine/orm/blob/2.13.x/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php#L110
instead on the class level.

And then use it in the phpdoc.

@@ -389,6 +426,49 @@ private function getInsertRevisionSQL(ClassMetadata $class): string
return $this->insertRevisionSQL[$class->name];
}

/**
* @param ClassMetadata $class
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* @param ClassMetadata $class
* @param ClassMetadata<object> $class

avoid the need of psalm-param

*
* @return string
*
* @psalm-param ClassMetadata<object> $targetClass
Copy link
Member

Choose a reason for hiding this comment

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

We prefer phpstan-param

@@ -403,34 +483,51 @@ private function saveRevisionEntityData(ClassMetadata $class, array $entityData,
if ($class->isInheritanceTypeJoined() && $class->isInheritedAssociation($field)) {
continue;
}
if (
($assoc['type'] & ClassMetadata::TO_ONE) === 0
if (!(($assoc['type'] & ClassMetadata::TO_ONE) === 0
Copy link
Member

Choose a reason for hiding this comment

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

Change !($a || $b ||$c) to !a && !b && !c

@VincentLanglet
Copy link
Member

@VincentLanglet I've checked the stan / psalm issues reported by the CI and I can fix them, no problem, but this will make the code changes in this PR even more complex, as there are quite a lot of things to review already. Shall I proceed with the fixes and throw them all in a separate commit? What would be the most convenient way to proceed?

Yes please fix the issues.

Lot of them where already fix on some others places of the code with some if checks or some assert. You can do the same.

@pietaj
Copy link
Contributor Author

pietaj commented Aug 24, 2022

@VincentLanglet I've checked the stan / psalm issues reported by the CI and I can fix them, no problem, but this will make the code changes in this PR even more complex, as there are quite a lot of things to review already. Shall I proceed with the fixes and throw them all in a separate commit? What would be the most convenient way to proceed?

Yes please fix the issues.

Lot of them where already fix on some others places of the code with some if checks or some assert. You can do the same.

Oh my, thanks for the ultra fast reply, I didn't mean to push ;) I will fix all issues and will come back to you after everything will pass green

@SonataCI
Copy link
Collaborator

SonataCI commented Sep 4, 2022

Could you please rebase your PR and fix merge conflicts?

@VincentLanglet
Copy link
Member

Any issue @pietaj ? :)

@pietaj
Copy link
Contributor Author

pietaj commented Sep 23, 2022

My only issue was my day job :) did the requested psalm / phpstan compliance cleanup and the requested changes

@VincentLanglet
Copy link
Member

My only issue was my day job :) did the requested psalm / phpstan compliance cleanup and the requested changes

Seems like build is still failing

@pietaj
Copy link
Contributor Author

pietaj commented Sep 23, 2022

That's odd ... both rector and phpstan pass on dev machine. I'll have a closer look

@VincentLanglet
Copy link
Member

That's odd ... both rector and phpstan pass on dev machine. I'll have a closer look

Tru to update your dependencies maybe ?

@pietaj
Copy link
Contributor Author

pietaj commented Sep 23, 2022

yeah, thanks for the tip. Outdated dependencies, I'll rerun and fix the rest

@pietaj
Copy link
Contributor Author

pietaj commented Sep 26, 2022

@VincentLanglet one of the failing checks is linked with rector, failing with

PHP Fatal error: Uncaught _PHPStan_9a6ded56a\Nette\DI\InvalidConfigurationException: The item 'conditionalTags › PHPStan\Rules\DisallowedConstructs\DisallowedLooseComparisonRule › phpstan.rules.rule' expects to be bool, array given. in phar:///home/runner/work/EntityAuditBundle/EntityAuditBundle/vendor/rector/rector/vendor/phpstan/phpstan/phpstan.phar/vendor/nette/di/src/DI/Compiler.php:219

I did some quick checks and the error is linked with phpstan/phpstan bumping from 1.8.5 => 1.8.6. Did you encounter a similar problems in other sonata projects as well?

@jordisala1991
Copy link
Member

jordisala1991 commented Sep 26, 2022

@VincentLanglet one of the failing checks is linked with rector, failing with

PHP Fatal error: Uncaught _PHPStan_9a6ded56a\Nette\DI\InvalidConfigurationException: The item 'conditionalTags › PHPStan\Rules\DisallowedConstructs\DisallowedLooseComparisonRule › phpstan.rules.rule' expects to be bool, array given. in phar:///home/runner/work/EntityAuditBundle/EntityAuditBundle/vendor/rector/rector/vendor/phpstan/phpstan/phpstan.phar/vendor/nette/di/src/DI/Compiler.php:219

I did some quick checks and the error is linked with phpstan/phpstan bumping from 1.8.5 => 1.8.6. Did you encounter a similar problems in other sonata projects as well?

Yes, ignore rector for now.

FYI, error already reported: rectorphp/rector#7509, waiting for a fix. (The most you can do is upgrade to rector ^0.14, to ensure we will get the fix.

Copy link
Member

@VincentLanglet VincentLanglet left a comment

Choose a reason for hiding this comment

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

It's ok for me, except some formatting issues.
Is this PR ok for you @jordisala1991 ?

Comment on lines 240 to 242
$tableAlias = $classMetadata->isInheritanceTypeJoined() && $classMetadata->isInheritedField(
$field
) && !$classMetadata->isIdentifier($field)
Copy link
Member

Choose a reason for hiding this comment

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

This reformatting is weird. I would prefer

Suggested change
$tableAlias = $classMetadata->isInheritanceTypeJoined() && $classMetadata->isInheritedField(
$field
) && !$classMetadata->isIdentifier($field)
$tableAlias = $classMetadata->isInheritanceTypeJoined()
&& $classMetadata->isInheritedField($field)
&& !$classMetadata->isIdentifier($field)

Comment on lines 420 to 422
$tableAlias = $classMetadata->isInheritanceTypeJoined() && $classMetadata->isInheritedField(
$field
) && !$classMetadata->isIdentifier($field)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
$tableAlias = $classMetadata->isInheritanceTypeJoined() && $classMetadata->isInheritedField(
$field
) && !$classMetadata->isIdentifier($field)
$tableAlias = $classMetadata->isInheritanceTypeJoined()
&& $classMetadata->isInheritedField($field)
&& !$classMetadata->isIdentifier($field)

Comment on lines 562 to 563
'INNER JOIN '.$tableName.' e ON r.id = e.'.$this->config->getRevisionFieldName(
).' WHERE '.$whereSQL.' ORDER BY r.id DESC';
Copy link
Member

Choose a reason for hiding this comment

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

this formatting is weird

Comment on lines 918 to 920
$otherEntityAssoc = $this->em->getClassMetadata(
$targetEntity
)->associationMappings[$mappedBy];
Copy link
Member

Choose a reason for hiding this comment

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

this formatting is weird

* @param ClassMetadata $class
* @param array<string, mixed> $assoc
*
* @psalm-param ClassMetadata<object> $targetClass
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* @psalm-param ClassMetadata<object> $targetClass
* @phpstan-param ClassMetadata<object> $targetClass

&& isset($assoc['relationToSourceKeyColumns'], $assoc['relationToTargetKeyColumns'], $assoc['joinTable']['name'])) {
$placeholders = ['?', '?'];

// $tableName = $this->config->getTableName($class);
Copy link
Member

Choose a reason for hiding this comment

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

Why adding a code commented ? can we remove it ?

Copy link
Member

@jordisala1991 jordisala1991 left a comment

Choose a reason for hiding this comment

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

Do we need more information on the readme, or should we only remove the last todo?

tests/RelationTest.php Outdated Show resolved Hide resolved
@jordisala1991
Copy link
Member

This Pr also needs a changelog.

The version bump is necessary because phpstan/phpstan bump
1.8.5 => 1.8.6 breaks rector < 0.14.4
@see rectorphp/rector#7509
VincentLanglet
VincentLanglet previously approved these changes Sep 28, 2022
@jordisala1991 jordisala1991 merged commit 5a98e1a into sonata-project:1.x Oct 8, 2022
@jordisala1991
Copy link
Member

Thanks @pietaj

@jordisala1991
Copy link
Member

I guess the todo list on the readme should be updated too.

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.

5 participants