doctrine / DoctrineORMModule

Doctrine ORM Module for Laminas
https://www.doctrine-project.org/projects/doctrine-orm-module.html
MIT License
439 stars 229 forks source link

Form element of inverse side of a bidirectional association is always removed #741

Open diniciacci opened 1 year ago

diniciacci commented 1 year ago

When a form is built from annotations (or attributes) if the form has an element that is an association, the element is removed from the form if it is on the inverse side of the association.

In case of a truly bidirectional (many-to-many) association implemented with best practices as suggested in https://gist.github.com/Ocramius/3121916 one might want to have forms generated from annotations (or attributes) to have the associated element on both sides i.e. also on the inverse side.

Currently EntityBasedFormBuilder checks to exclude an element here that triggers handleExcludeAssociation and this just checks if the element is the inverse side, and in that case, as a consequence, removes the element from the $formSpec.

This should be configurable, maybe with an appropriate annotation, ending up in the $metadata, that can be checked in the handleExcludeAssociation().

The annotation can be something like "bidirectional", to not mix the term with words like 'required', 'force' or 'keep'.

TomHAnderson commented 1 year ago

Would detaching the handleExcludeAssociation do the job you're asking for? https://github.com/doctrine/DoctrineORMModule/blob/05751818bb579c94ca8ff077332f98f9abf15021/src/Form/Annotation/DoctrineAnnotationListener.php#L66

diniciacci commented 1 year ago

Yes it should, but if I'm not wrong then unidirectional associations will get the element included too. I.e. Removing the listener has a global effect on the form generation. I.e. I can't easily remove it and re-add it during a single form generation

The common case is a composed form that has a bidirectional association and also contains another association that is unidirectional. If i remove the listener then both association will be included instead of just one.

In case of single form generation: removing, generating a form, re-adding workflow also looks a bit cumbersome to me.

I.e. IMHO the solution should be specific and not global. There should be a way to specify to not exclude an association (that is correctly excluded by default).

TomHAnderson commented 1 year ago

I agree a specific solution is in order. So there are two options.

  1. change the whole library to suit your specific need (hasn't been raised before)
  2. detach the default listener and attach your own custom listener that looks for your specific use case.

I much prefer 2

diniciacci commented 1 year ago

Dear Tom, not sure I get the point of you "answer".

I reported a use case that is not covered by the library and also a use case where detaching the listener would not work and has no reasonable workaround. Hence, to me, the request to support form generation of bidirectional associations look reasonable and not a corner case of my specific implementation.

To sum up the current status of the library:

  1. form generation of a form containing one or more many-to-many bidirectional associations is not supported by default. Workaround is to detach the listener and re-attach it afterwards.
  2. form generation of a form containing one or more many-to-many bidirectional associations and one or more many-to-many unidirectional associations is not supported and there is no workaround.

Do we agree on this or I am misunderstanding something?

TomHAnderson commented 12 months ago

This issue requires a lot of framework to duplicate the issue. I would appreciate it if you'd provide a working application that duplicates this issue.