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 accessor property instead of code #6837

Merged
merged 3 commits into from
Feb 9, 2021

Conversation

VincentLanglet
Copy link
Member

Subject

I am targeting this branch, because BC.

Closes #6831.
Closes #6741.

Changelog

### Added
- `FieldDescription` `accessor` option

### Deprecated
- `FieldDescription` `code` option

@VincentLanglet VincentLanglet requested a review from a team February 4, 2021 15:31
dmaicher
dmaicher previously approved these changes Feb 4, 2021
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.

would this work with a path? like

'accessor' => 'children.name'

@VincentLanglet
Copy link
Member Author

would this work with a path? like

'accessor' => 'children.name'

Like it's implemented, no.
It handle a callable or the name of the callable. countChildren for instance.
But the code option does not support path.

We could add support for path, but:

  • 'accessor' => 'countChildren' could lead to unexpected behavior if someone has both a method countChildren and getCountChildren (my example is stupid but you get the idea). Which one are you expecting first ?
  • Why not passing ->add('children.name') directly ? Do you have a usecase in mind ?

Should I rename the option to accessor_method to make it clearer ?

@franmomu
Copy link
Member

franmomu commented Feb 5, 2021

I understand the idea, but I think that's unusual, what I would expect is to behave the same way that the form component does, some examples that I think they should work:

->add('address.name');
->add('addressName', null, ['accessor' => 'address.name']);
->add('firstAddressName', null, ['accessor' => 'addresses[0].name']);
->add('thumbnail', null, ['accessor' => 'image[thumbnail]']);

@VincentLanglet
Copy link
Member Author

I understand the idea, but I think that's unusual, what I would expect is to behave the same way that the form component does, some examples that I think they should work:

->add('address.name');
->add('addressName', null, ['accessor' => 'address.name']);
->add('firstAddressName', null, ['accessor' => 'addresses[0].name']);
->add('thumbnail', null, ['accessor' => 'image[thumbnail]']);

Should we allow

  • either a method
  • either a propertyPath
    And stop using the method name ? @sonata-project/contributors

@VincentLanglet VincentLanglet requested a review from a team February 7, 2021 13:58
@VincentLanglet
Copy link
Member Author

@vladyslavstartsev I know you're using this property (or at least you tried).
What sounds natural to you ?

@vv12131415
Copy link
Contributor

IMO
I think that method is more natural. Yes, it's still "magic", but it's still less "magic" than propertyPath

@vv12131415
Copy link
Contributor

but, since this option will void SRP, maybe it will be better to do, 2 separate options

accessor_method and accessor_path

WDYT?

@VincentLanglet
Copy link
Member Author

but, since this option will void SRP, maybe it will be better to do, 2 separate options

accessor_method and accessor_path

WDYT?

With two options we have to handle the case when both are set.

I don't think it's again SRP, for instance symfony allow both a property path or a method for the choice_value option

phansys
phansys previously approved these changes Feb 8, 2021
@VincentLanglet
Copy link
Member Author

The only things to decide here is

Allowing Method + PropertyPath VS Allowing Method + Method name

@vv12131415
Copy link
Contributor

my vote is for Method name

@VincentLanglet
Copy link
Member Author

my vote is for Method name

Do you have examples in your code, where property path wouldn't work ?

@VincentLanglet VincentLanglet dismissed stale reviews from phansys and dmaicher via f91592e February 8, 2021 16:28
@VincentLanglet
Copy link
Member Author

my vote is for Method name

Do you have examples in your code, where property path wouldn't work ?

Since I don't have in mind (and this can still be done by passing the method), I allowed property path instead of method name @franmomu

dmaicher
dmaicher previously approved these changes Feb 8, 2021
dmaicher
dmaicher previously approved these changes Feb 8, 2021
@VincentLanglet
Copy link
Member Author

Sorry @dmaicher I forgot to update the doc ^^'

dmaicher
dmaicher previously approved these changes Feb 8, 2021
@SonataCI
Copy link
Collaborator

SonataCI commented Feb 9, 2021

Could you please rebase your PR and fix merge conflicts?

wbloszyk
wbloszyk previously approved these changes Feb 9, 2021
@wbloszyk wbloszyk dismissed stale reviews from dmaicher and themself via 2868774 February 9, 2021 11:42
wbloszyk
wbloszyk previously approved these changes Feb 9, 2021
@wbloszyk wbloszyk requested review from dmaicher and a team February 9, 2021 11:43
@VincentLanglet VincentLanglet merged commit 49aa482 into sonata-project:3.x Feb 9, 2021
@VincentLanglet VincentLanglet deleted the deprecateCode branch February 9, 2021 12:51
.' since sonata-project/admin-bundle 3.x and will throw an exception in 4.0.',
\E_USER_DEPRECATED
);
@trigger_error(
Copy link
Member

Choose a reason for hiding this comment

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

Since code is always set in the persistence bundles, this is always triggered: https://github.com/sonata-project/SonataDoctrineORMAdminBundle/runs/1903176971?check_suite_focus=true#step:8:108

Copy link
Member Author

Choose a reason for hiding this comment

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

I think we should remove the code in the persistence bundle.

If the name is getfoo and the fieldName is bar, because we're passing the code, the value will be $object->getFoo() instead of relying on bar ; this seems to be a bug to me.

Choose a reason for hiding this comment

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

👍

@toooni
Copy link
Contributor

toooni commented Feb 19, 2021

@VincentLanglet I've got an issue because of this change. On PHP8 this gives me an error date(): Argument #1 ($format) must be of type string, App\Entity\Invoice given. This is happening because of return $accessor($object); where 'date' is will be used as the function name.

    protected function configureListFields(ListMapper $listMapper): void
    {
        $listMapper->add('date', 'date', ['label' => 'Datum']);
    }

EDIT: This is happening because the name of the field is also a function in PHP (date in this case). (https://github.com/sonata-project/SonataAdminBundle/pull/6837/files#diff-d470daa9a101ba3f927eb0b89745013568722e7e3cc00dc0a340438b64b06d32R445)

@VincentLanglet
Copy link
Member Author

@toooni This should have been fixed by https://github.com/sonata-project/SonataAdminBundle/pull/6859/files

Can you be more explicit ?
What is your entity ? Which getter is supposed to be called ?

@toooni
Copy link
Contributor

toooni commented Feb 19, 2021

@VincentLanglet Oh, sorry. I've updated the day before the fix.

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