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

Prevent false deprecation error for n to m associations #6411

Merged
merged 4 commits into from
Feb 22, 2021

Conversation

jorrit
Copy link
Contributor

@jorrit jorrit commented Sep 24, 2020

Subject

CRUDController->checkParentChildAssociation() checks if the associated item of the child admins subject equals the parent admins subject. This doesn't work when the parent relations is many-to-many as the child property will be a Collection and not the parent item itself. In that case, the parent item must be searched in this collection.

I am targeting this branch, because its a backwards compatible fix.

Changelog

### Fixed
- Prevent false deprecation notice when accessing an item in a child admin that has a Many to Many relationship with the parent item.

VincentLanglet
VincentLanglet previously approved these changes Sep 24, 2020
@jorrit jorrit force-pushed the parentchildmanymany branch 3 times, most recently from d23fbb3 to 4b3945d Compare September 29, 2020 08:17
@jorrit jorrit force-pushed the parentchildmanymany branch 3 times, most recently from 5757950 to 90c6c5d Compare October 1, 2020 13:10
Copy link
Member

@franmomu franmomu left a comment

Choose a reason for hiding this comment

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

These are suggestions.

@@ -1554,7 +1554,13 @@ private function checkParentChildAssociation(Request $request, object $object):
$propertyAccessor = PropertyAccess::createPropertyAccessor();
$propertyPath = new PropertyPath($this->admin->getParentAssociationMapping());

if ($parentAdmin->getObject($parentId) !== $propertyAccessor->getValue($object, $propertyPath)) {
$parentObject = $parentAdmin->getObject($parentId);
Copy link
Member

Choose a reason for hiding this comment

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

What about calling this $parentAdminObject? And the one below $parentObject. To be honest, I can't come up with something better, but $parentObject and $objectParent sound strange.

Copy link
Member

Choose a reason for hiding this comment

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

parentAdminObject will be OK.
objectParent is bad idea, use parentAdminAvailableObjects instead.

*
* @return bool true when $haystack equals $needle or $haystack is iterable and contains $needle
*/
private static function equalsOrInList(object $needle, $haystack): bool
Copy link
Member

Choose a reason for hiding this comment

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

Even if it doesn't need $this, is there any benefit to create it as static?

Looking at the method name, what about:

Suggested change
private static function equalsOrInList(object $needle, $haystack): bool
private function hasParentChildAssociation(object $parent, $parentAssociation): bool

Copy link
Member

Choose a reason for hiding this comment

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

If this is static, it could be in another class instead of this big CRUDController.

Copy link
Member

@wbloszyk wbloszyk Jan 12, 2021

Choose a reason for hiding this comment

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

IMHO we can keep it in CRUDController and add it to #6512.
Of course remove static.

src/Controller/CRUDController.php Outdated Show resolved Hide resolved
@VincentLanglet VincentLanglet added this to the 4.0 milestone Dec 24, 2020
@VincentLanglet
Copy link
Member

Hi @jorrit, are you still interested in this PR ?

@jorrit
Copy link
Contributor Author

jorrit commented Jan 12, 2021

Yes, sorry for not responding. Can you recommend a class to put the static method into?

@jorrit jorrit force-pushed the parentchildmanymany branch from 90c6c5d to 869c460 Compare January 12, 2021 09:47
@VincentLanglet
Copy link
Member

Can you recommend a class to put the static method into?

cc @sonata-project/contributors ; Any idea ?

Copy link
Member

@wbloszyk wbloszyk left a comment

Choose a reason for hiding this comment

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

@jorrit I add some comments.

*
* @return bool true when $haystack equals $needle or $haystack is iterable and contains $needle
*/
private static function equalsOrInList(object $needle, $haystack): bool
Copy link
Member

@wbloszyk wbloszyk Jan 12, 2021

Choose a reason for hiding this comment

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

IMHO we can keep it in CRUDController and add it to #6512.
Of course remove static.

@@ -1554,7 +1554,13 @@ private function checkParentChildAssociation(Request $request, object $object):
$propertyAccessor = PropertyAccess::createPropertyAccessor();
$propertyPath = new PropertyPath($this->admin->getParentAssociationMapping());

if ($parentAdmin->getObject($parentId) !== $propertyAccessor->getValue($object, $propertyPath)) {
$parentObject = $parentAdmin->getObject($parentId);
Copy link
Member

Choose a reason for hiding this comment

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

parentAdminObject will be OK.
objectParent is bad idea, use parentAdminAvailableObjects instead.

@jorrit jorrit force-pushed the parentchildmanymany branch from 869c460 to 2a6a064 Compare January 19, 2021 10:04
@jorrit
Copy link
Contributor Author

jorrit commented Jan 19, 2021

@wbloszyk : thanks, I've made the changes you suggested.

@VincentLanglet VincentLanglet requested review from wbloszyk and a team January 19, 2021 12:32
VincentLanglet
VincentLanglet previously approved these changes Jan 31, 2021
@VincentLanglet
Copy link
Member

Please review @sonata-project/contributors @wbloszyk

@VincentLanglet VincentLanglet requested a review from a team February 22, 2021 20:56
src/Controller/CRUDController.php Outdated Show resolved Hide resolved
src/Controller/CRUDController.php Outdated Show resolved Hide resolved
src/Controller/CRUDController.php Outdated Show resolved Hide resolved
@VincentLanglet VincentLanglet merged commit 6a7c306 into sonata-project:3.x Feb 22, 2021
@VincentLanglet
Copy link
Member

Thanks @jorrit

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.

6 participants